Knex迁移不会创建表

时间:2018-05-24 13:08:09

标签: knex.js

我有一些看起来像这样的Knex迁移脚本:

'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'));
            }
        });
};

我第一次运行这个脚本时,它创建了一个表,这很棒,但后来我注意到表格格式错误(我传递了错误的参数。)所以我从数据库中手动删除了创建的表,使用knex_migrations和knex_migrations_lock表,并重新运行迁移命令。现在,它告诉我所有迁移都已运行,但我仍然没有看到该表。是什么赋予了?是否有一段配置隐藏在我需要删除的地方?

1 个答案:

答案 0 :(得分:3)

添加“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'));
            }
        });
};
相关问题