Laravel 中的 SQLSTATE[HY000] 错误:无法添加外键
SQLSTATE[HY000] Error in Laravel: failing to add foreign key
我已经尝试了这里几乎所有的答案,我的迁移是有序的,我已经将引擎设置为 'InnoDB',我已经将 user_types_id
设置为无符号。我想我错过了一些东西,我无法迁移它。 (虽然我能够从 phpMyAdmin 手动添加 FK)。
我的 user-types
是在我的用户 table 之前创建的,我确实确保了 :)
迁移:
Schema::create('user-types',function(Blueprint $table){
$table->engine = 'InnoDB';
$table->increments('id');
$table->string('name');
$table->timestamps();
});
和用户 table:
// I think I saw this somewhere, too
Schema::enableForeignKeyConstraints();
Schema::create('users',function(Blueprint $table){
$table->engine = 'InnoDB';
$table->increments('id');
$table->string('name');
$table->string('password');
$table->integer('user_types_id')->unsigned()->nullable();
$table->timestamps();
});
Schema::table('users',function(Blueprint $table){
// When I index it I am able to manually add fk from
// phpMyAdmin
$table->index('user_types_id');
$table->foreign('user_types_id')->references('id')->on('user_types');
});
您的架构定义中存在名称差异。
您正在使用 Schema::create('user-types',function(....
创建 table,其中 table 名称是 user-types
,但是当您设置外键时,您传递的是 user_types
因此,让它们相同,以正确者为准。 user-types
或 user_types
都行。
你也可以这样参考
Schema::create('users',function(Blueprint $table){
$table->engine = 'InnoDB';
$table->increments('id');
$table->foreign('user_types_id')->references('id')->on('user-types')->onDelete('cascade');
$table->string('name');
$table->string('password');
$table->integer('user_types_id')->nullable();
$table->timestamps();
});
Schema::enableForeignKeyConstraints(); //Remove this its not require,
我认为你需要写,
Schema::disableForeignKeyConstraints(); // disable foreign key checks while creating tables which are dependant on each other
在您遇到此问题的模式定义的顶部,它应该有效。
我已经尝试了这里几乎所有的答案,我的迁移是有序的,我已经将引擎设置为 'InnoDB',我已经将 user_types_id
设置为无符号。我想我错过了一些东西,我无法迁移它。 (虽然我能够从 phpMyAdmin 手动添加 FK)。
我的 user-types
是在我的用户 table 之前创建的,我确实确保了 :)
迁移:
Schema::create('user-types',function(Blueprint $table){
$table->engine = 'InnoDB';
$table->increments('id');
$table->string('name');
$table->timestamps();
});
和用户 table:
// I think I saw this somewhere, too
Schema::enableForeignKeyConstraints();
Schema::create('users',function(Blueprint $table){
$table->engine = 'InnoDB';
$table->increments('id');
$table->string('name');
$table->string('password');
$table->integer('user_types_id')->unsigned()->nullable();
$table->timestamps();
});
Schema::table('users',function(Blueprint $table){
// When I index it I am able to manually add fk from
// phpMyAdmin
$table->index('user_types_id');
$table->foreign('user_types_id')->references('id')->on('user_types');
});
您的架构定义中存在名称差异。
您正在使用 Schema::create('user-types',function(....
创建 table,其中 table 名称是 user-types
,但是当您设置外键时,您传递的是 user_types
因此,让它们相同,以正确者为准。 user-types
或 user_types
都行。
你也可以这样参考
Schema::create('users',function(Blueprint $table){
$table->engine = 'InnoDB';
$table->increments('id');
$table->foreign('user_types_id')->references('id')->on('user-types')->onDelete('cascade');
$table->string('name');
$table->string('password');
$table->integer('user_types_id')->nullable();
$table->timestamps();
});
Schema::enableForeignKeyConstraints(); //Remove this its not require,
我认为你需要写,
Schema::disableForeignKeyConstraints(); // disable foreign key checks while creating tables which are dependant on each other
在您遇到此问题的模式定义的顶部,它应该有效。