knex和MYSQL js错误ER_CANNOT_ADD_FOREIGN:无法添加外键约束

问题描述

我在尝试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
我想做的是有一个表,该表具有来自两个不同表的两个外键。因此,这里有外表要放置的用户和todos表。但是,仅用户外键有效,如果我添加其他外键,则会收到上述错误。任何帮助,将不胜感激!另外,knex已安装到node_modules中。这是代码,也不是类似的问题。唯一类似的就是这个

KNEX & MYSQL - Error: ER_CANNOT_ADD_FOREIGN: Cannot add foreign key constraint
但是,不幸的是,在我的情况下,答案不起作用...

用户

    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表

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

板桌

    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的顺序构建每个表。

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