I'm trying to run a simple test using Mocha and my Bookshelf / Knex model, however I'm getting the error "Unhandled rejection Error: SQLITE_ERROR: no such table: users". Note that I'm trying to use in memory SQLite.
Here's my Knexfile:
module.exports = {
development: {
client: 'sqlite3',
connection: {
filename: ':memory:'
}
},
test: {
client: 'mysql',
connection: {
host: '172.18.0.2',
user: 'root',
password: '',
database: 'staging_db',
charset : 'utf8'
},
pool: {
min: 2,
max: 10
},
migrations: {
tableName: 'knex_migrations'
}
},
production: {
client: 'mysql',
connection: {
host: process.env.DB_HOST || 'localhost',
user: process.env.DB_USER || 'usr',
password: process.env.DB_PWD || '',
database: process.env.DB_NAME || 'db',
charset : 'utf8'
},
pool: {
min: 2,
max: 10
},
migrations: {
tableName: 'knex_migrations'
}
}
};
Database.js:
var config = require('../knexfile.js');
var env = process.env.NODE_ENV || 'development';
var knex = require('knex')(config[env]);
knex.migrate.latest([config]);
let bookshelf = require('bookshelf')(knex);
bookshelf.plugin('registry'); // Resolve circular dependencies with relations
// Export bookshelf for use elsewhere
module.exports = bookshelf;
And my User model:
let bookshelf = require('../config/database');
require('./role');
var User = bookshelf.Model.extend({
tableName: 'users',
role: function() {
return this.hasOne(Role);
}
});
module.exports = bookshelf.model('user', User);
And my test:
var User = require('../../models/user'),
chai = require('chai'),
expect = chai.expect;
describe('User model', function () {
it('should return empty set before adding anything', () => {
expect(User.collection().count()).to.equal(0);
});
});
Am I missing something?
UPDATE
Adding the migration file:
exports.up = function(knex, Promise) {
return knex.schema.createTable('roles', function (table) {
//this creates an id column as auto incremented primary key
table.increments();
table.string('description', 45).notNullable();
})
.createTable('users', function (table) {
table.increments();
table.string('name', 60);
table.string('password', 45);
table.integer('role_id').unsigned();
table.foreign('role_id').references('roles.id');
});
};
exports.down = function(knex, Promise) {
return knex.schema.dropTable('users').dropTable('roles');
};