我正在使用 Laravel 6。我创建了一些迁移,但我无法让它们成功运行。这些是我的迁移。
public function up()
{
Schema::create('nationalities', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->timestamps();
$table->engine = "InnoDB";
});
public function up()
{
Schema::create('genders', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->timestamps();
$table->engine = "InnoDB";
});
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('firstname');
$table->string('lastname');
$table->string('street');
$table->string('zip');
$table->string('city');
$table->date('birthdate');
$table->string('gender');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
$table->unsignedBigInteger('gender_id');
$table->unsignedBigInteger('nationality_id');
$table->engine = "InnoDB";
$table->foreign('gender_id')->references('id')->on('gender');
$table->foreign('nationality_id')->references('id')->on('nationality');
});
}
执行php artisan migrate后,我收到此错误消息:
General error: 1215 Cannot add foreign key constraint (SQL: alter table
users
add constraintusers_gender_id_foreign
foreign key (gender_id
) referencesgender
(id
))
我做错了什么?
最佳答案
您的表格名称是genders
而不是gender
,并且nationalities
不是nationality
:
更改:
...->references('id')->on('gender');
...->references('id')->on('nationality');
致:
...->references('id')->on('genders');
...->references('id')->on('nationalities');
关于mysql - Laravel 抛出一般错误 : 1215 Cannot add foreign key constraint"when I create foreign keys,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58845993/