knex & MYSQL js error ER_CANNOT_ADD_FOREIGN: 无法添加外键约束

knex & MYSQL js error ER_CANNOT_ADD_FOREIGN: Cannot add foreign key constraint

我在尝试 knex migrate:latest 时遇到问题

migration file "20200701012817_personal_todos.ts" failed
migration failed with error: alter table `personal_todos` add constraint `personal_todos_board_id_foreign` foreign key (`board_id`) references `personal_todo_board` (`id`) on update CASCADE on delete CASCADE - ER_CANNOT_ADD_FOREIGN: Cannot add foreign key constraint
我想要做的是有一个 table,它有两个来自两个不同 table 的外键。因此,用户和待办事项 tables 是外键所在的位置。但是,只有用户外键有效,如果我添加其他外键,则会出现上述错误。任何帮助,将不胜感激!此外,knex 安装到 node_modules... 这是代码,也不是类似的问题。唯一相似的是这个

KNEX & MYSQL - Error: ER_CANNOT_ADD_FOREIGN: Cannot add foreign key constraint
然而,不幸的是,这个答案对我不起作用...

用户table

    import * as Knex from "knex";
    
    
    export async function up(knex: Knex): Promise<any> {
        return knex.schema.createTable('users', (table)=>{
            table.increments('id').primary();
            table.string('first_name');
            table.string('last_name');
            table.string('username').notNullable();
            table.string('email');
            table.string('password').notNullable();
            table.integer('age');
            table.string('bio');
            table.string('gender');
            table.string('personalsecret1');
            table.string('personalsecret2');
            table.string('personalsecret3');
            table.binary('img');
            table.string('colorScheme');
            table.timestamps(true,true);
            table.boolean('payed');
            table.boolean('active');
            table.boolean('friends_can_see_private');
        })
    }
    
    
    export async function down(knex: Knex): Promise<any> {
        return knex.schema.dropTableIfExists('users')
    }

todos table 是问题发生的地方

    import * as Knex from "knex";
    
    export async function up(knex: Knex): Promise<any> {
        return knex.schema.createTable('personal_todos', (table)=>{
            table.increments('id').primary();
            // I tried unsigned and it didn't work
            table.integer('user_id').unsigned().notNullable();
            table.integer('board_id').unsigned().notNullable();
    
            table.foreign('user_id').references('users.id');
            // here is the problem table.foreign('board_id').references('id').inTable('personal_todo_board').onUpdate('CASCADE').onDelete('CASCADE');
            
            table.boolean('active').notNullable();
            table.string('start_time');
            table.string('end_time');
            table.string('header');
            table.string('body');
            table.integer('container_index').notNullable();
            table.integer('container_item_index');
            table.timestamps(true,true);
            table.boolean('private').notNullable();
        })
    }
    
    
    export async function down(knex: Knex): Promise<any> {
        return knex.schema.dropTableIfExists('personal_todos');
    }

棋盘table

    import * as Knex from "knex";
    
    export async function up(knex: Knex): Promise<any> {
        return knex.schema.createTable('personal_todo_board', (table)=>{
            table.increments('id').primary();
            table.integer('user_id').unsigned().notNullable();
            table.foreign('user_id').references('users.id').onUpdate('CASCADE').onDelete('CASCADE');
            table.string('header').notNullable();
            table.string('last_active');
            table.string('small_description');
            table.timestamps( true, true);
            table.boolean('private').notNullable();
        })
    }
    
    
    export async function down(knex: Knex): Promise<any> {
        return knex.schema.dropTableIfExists('personal_todo_board')
    }

我发现文件是这样制作的。 Knex 按照您执行此命令 js knex migrate:make table_name.

的顺序构建每个 table

这意味着您必须按照您引用的顺序使用该命令。如果您要引用另一个 table,请确保它是之前创建的,否则您将遇到外键约束错误。如果你碰巧 运行 进入这个错误,那么复制之后的 table 并且有外键并删除旧的(删除文件)。然后使用 js knex migrate:make table_name 命令制作另一个同名的并粘贴到您的旧 table。如果您使用的是种子,请对种子执行相同的操作。确保回滚并删除并重新创建您的数据库!