Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Knex migration does not create tables

Tags:

knex.js

I've got some Knex migration scripts that look like this:

'use strict';
exports.up = function (knex) {
    return knex.schema
        .hasTable('user')
        .then(function (exists) {
            if (!exists) {
                knex
                    .schema
                    .createTable('user', function (table) {
                        table.increments('id').primary();
                        table.string('firstName');
                        table.string('lastName');
                        table.string('email');
                        table.string('password');
                        table.string('username');
                    })
                    .then(console.log('created user table'));
            }
        });
};

The first time I ran this script, it created a table, which was great, but then I noticed that the table was malformed (I had passed a wrong parameter.) So I manually deleted the created table from my database, along with the knex_migrations and knex_migrations_lock tables, and re-ran the migration command. Now, it tells me that all migrations have run, but I still don't see the table. What gives? Is there a piece of of config hiding somewhere that I need to delete?

like image 730
Boris K Avatar asked Jun 03 '26 22:06

Boris K


1 Answers

add "return".

'use strict';
exports.up = function (knex) {
    return knex.schema
        .hasTable('user')
        .then(function (exists) {
            if (!exists) {
              return knex // **** udpate
                    .schema
                    .createTable('user', function (table) {
                        table.increments('id').primary();
                        table.string('firstName');
                        table.string('lastName');
                        table.string('email');
                        table.string('password');
                        table.string('username');
                    })
                    .then(console.log('created user table'));
            }
        });
};
like image 101
Fazal Rasel Avatar answered Jun 05 '26 22:06

Fazal Rasel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!