Javascript Knex.js列引用外键don';无法在迁移期间创建

Javascript Knex.js列引用外键don';无法在迁移期间创建,javascript,knex.js,Javascript,Knex.js,当我执行knex.js迁移时,除了下面“recipe component”连接表中的两个外键表之外,所有内容都会生成。有人知道我做错了什么吗?这是迁移中的第三个表: exports.up = function(knex, Promise) { return Promise.all([ knex.schema.hasTable('recipe').then((exists) => { console.log('does knex have re

当我执行knex.js迁移时,除了下面“recipe component”连接表中的两个外键表之外,所有内容都会生成。有人知道我做错了什么吗?这是迁移中的第三个表:

exports.up = function(knex, Promise) {
    return Promise.all([
        knex.schema.hasTable('recipe').then((exists) => {
            console.log('does knex have recipe table?', exists);
            if (!exists) {
                return knex.schema.createTable('recipe', (table) => {
                    table.uuid('id');
                    table.string('name');
                    table.string('description');
                })
            }
        }),
        knex.schema.hasTable('ingredient').then((exists) => {
            console.log('does knex have ingredient table?', exists);
            if (!exists) {
                return knex.schema.createTable('ingredient', (table) => {
                    table.uuid('id');
                    table.string('name');
                })
            }
        }),
        knex.schema.hasTable(`recipe-ingredient`).then((exists) => {
            console.log('does knex have recipe-ingredient table?', exists);
            if (!exists) {
                return knex.schema.createTable(`recipe-ingredient`, (table)=> {
                    table.uuid('recipe_id').references('id').inTable('recipe').notNull();
                    table.uuid('ingredient_id').references('id').inTable('ingredient').notNull();
                    table.string('qty');  // <-- chose string instead of int because receipes include qty such as '1/3 cup', '1 teaspoon', etc.
                })
            }
        })
    ])
};

exports.down = function(knex, Promise) {
    return Promise.all([
        knex.schema.dropTable('recipe-ingredient'),
        knex.schema.dropTable('ingredient'),
        knex.schema.dropTable('recipe')
    ])
};

请注意,.env文件中的变量没有什么特别之处。只有基本用户名、密码和数据库名。

您不应该使用promise.all并并行运行所有查询

IIRC knex为这些查询创建事务,但实际上mysql在第一个
CREATE TABLE
语句之后执行隐式提交,因此事务将被提交,其余的表创建可能以多种方式失败

Knex可能会说,它无法对提交的事务执行更多查询,或者它可能只是忽略其他查询,或者它可能只是在事务之外通过相同的数据库连接运行它们。很难说当您运行迁移时会发生什么,但它肯定不会完全实现您所希望的

这应该更好地发挥作用:

exports.up = function(knex, Promise) {
    return knex.schema.hasTable('recipe').then((exists) => {
        console.log('does knex have recipe table?', exists);
        if (!exists) {
            return knex.schema.createTable('recipe', (table) => {
                table.uuid('id');
                table.string('name');
                table.string('description');
            });
        }
    }).then(() => {
        return knex.schema.hasTable('ingredient').then((exists) => {
            console.log('does knex have ingredient table?', exists);
            if (!exists) {
                return knex.schema.createTable('ingredient', (table) => {
                    table.uuid('id');
                    table.string('name');
                })
            }
        });
    }).then(() => {
        return knex.schema.hasTable(`recipe-ingredient`).then((exists) => {
            console.log('does knex have recipe-ingredient table?', exists);
            if (!exists) {
                return knex.schema.createTable(`recipe-ingredient`, (table)=> {
                    table.uuid('recipe_id').references('id').inTable('recipe').notNull();
                    table.uuid('ingredient_id').references('id').inTable('ingredient').notNull();
                    table.string('qty');  // <-- chose string instead of int because receipes include qty such as '1/3 cup', '1 teaspoon', etc.
                })
            }
        })
    });
};

exports.down = function(knex, Promise) {
    return knex.schema.dropTable('recipe-ingredient')
      .then(() => knex.schema.dropTable('ingredient'))
      .then(() => knex.schema.dropTable('recipe'));
};
exports.up=函数(knex,Promise){
返回knex.schema.hasTable('recipe')。然后((存在)=>{
log('knex是否有配方表?',存在);
如果(!存在){
返回knex.schema.createTable('recipe',(table)=>{
表.uuid('id');
table.string('name');
table.string('description');
});
}
}).然后(()=>{
返回knex.schema.hasTable('component')。然后((exists)=>{
console.log('knex是否有成分表?',存在);
如果(!存在){
返回knex.schema.createTable('component',(table)=>{
表.uuid('id');
table.string('name');
})
}
});
}).然后(()=>{
返回knex.schema.hasTable(`recipe component`)。然后((exists)=>{
console.log('knex是否有配方成分表?',存在);
如果(!存在){
返回knex.schema.createTable(`recipe component`,(table)=>{
table.uuid('recipe_id')。references('id')。inTable('recipe')。notNull();
table.uuid('component_id')。references('id')。inTable('component')。notNull();
table.string('qty');//knex.schema.dropTable('component'))
.then(()=>knex.schema.dropTable('recipe');
};

Does
console.log('knex是否有配方成分表?',存在)
执行?存在的
的值是多少?@therobinkim伟大的问题。以前那些日志打印为true,现在它们都打印为false:
knex有配方表吗?false knex有配方表吗?false knex有配方表吗?false
迁移失败,
ER\u不能添加外部:不能添加外键约束
@therobinkim即使收到失败消息,表也在生成,包括外键列,种子现在也在工作!!!哪个数据库?在这种情况下,这可能也很重要。@MikaelLepistö我会将我的knexfile添加到帖子中,以便您可以看到。它只是mySQL的一个本地实例。谢谢非常感谢@Mikael Lepisto。很抱歉没有早点回到这个帖子。从那时起,我不得不离开这个项目。
exports.up = function(knex, Promise) {
    return knex.schema.hasTable('recipe').then((exists) => {
        console.log('does knex have recipe table?', exists);
        if (!exists) {
            return knex.schema.createTable('recipe', (table) => {
                table.uuid('id');
                table.string('name');
                table.string('description');
            });
        }
    }).then(() => {
        return knex.schema.hasTable('ingredient').then((exists) => {
            console.log('does knex have ingredient table?', exists);
            if (!exists) {
                return knex.schema.createTable('ingredient', (table) => {
                    table.uuid('id');
                    table.string('name');
                })
            }
        });
    }).then(() => {
        return knex.schema.hasTable(`recipe-ingredient`).then((exists) => {
            console.log('does knex have recipe-ingredient table?', exists);
            if (!exists) {
                return knex.schema.createTable(`recipe-ingredient`, (table)=> {
                    table.uuid('recipe_id').references('id').inTable('recipe').notNull();
                    table.uuid('ingredient_id').references('id').inTable('ingredient').notNull();
                    table.string('qty');  // <-- chose string instead of int because receipes include qty such as '1/3 cup', '1 teaspoon', etc.
                })
            }
        })
    });
};

exports.down = function(knex, Promise) {
    return knex.schema.dropTable('recipe-ingredient')
      .then(() => knex.schema.dropTable('ingredient'))
      .then(() => knex.schema.dropTable('recipe'));
};