Laravel 迁移添加外键给出错误外键 (`user_id`)
Laravel migration add foreign key gives error foreign key (`user_id`)
当我 php artisan migrate
时出现错误,请参见下文。
Order migrations 是 users, companys 和 pivotmigration.
当我删除 user
时,所有 companies
都必须删除,当我删除 company
时,所有 users
都必须删除。
我做错了什么?
User.php
Schema::create('users', function (Blueprint $table) {
$table->engine = "InnoDB";
$table->bigIncrements('id');
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
Companies.php
Schema::create('companies', function (Blueprint $table) {
$table->engine = "InnoDB";
$table->increments('id')->unsigned();
$table->string('companyname');
$table->string('address');
$table->integer('housenumber');
$table->string('postalcode');
$table->string('city');
$table->string('province');
$table->string('email');
$table->string('phonenumber');
$table->timestamps();
});
CreateUserCompanyPivotTable.php
Schema::create('user_company', function (Blueprint $table) {
$table->engine = "InnoDB";
$table->integer('user_id')->unsigned();
$table->integer('company_id')->unsigned();
});
Schema::table('user_company', function (Blueprint $table) {
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->foreign('company_id')->references('id')->on('companies')->onDelete('cascade');
});
错误:
Illuminate\Database\QueryException : SQLSTATE[HY000]: General error: 1215
Cannot add foreign key constraint (SQL: alter table `user_company` add constraint `user_company_user_id_foreign` foreign key (`user_id`) references `users` (`id`) on delete cascade)
迁移失败,因为 user_company
中 user_id
的数据类型与 users
中的 id
不匹配。
您在 users
table 中使用了 bigIncrement()
,它创建了一个类型为 UNSIGNED BIGINT
的 auto_increment
字段。
在 user_company
table 中,您使用 integer()->unsigned()
创建 user_id
,这会创建一个 auto_increment
字段,类型为 UNSIGNED INT
。
MySQL 创建外键时需要两个字段类型相同。
要解决此问题,您应该用 bigInteger()->unsigned()
创建 user_id
。
Schema::create('user_company', function (Blueprint $table) {
$table->engine = "InnoDB";
$table->bigInteger('user_id')->unsigned();
$table->bigInteger('company_id')->unsigned();
// You don't need separate schema code to create foreign
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->foreign('company_id')->references('id')->on('companies')->onDelete('cascade');
});
当我 php artisan migrate
时出现错误,请参见下文。
Order migrations 是 users, companys 和 pivotmigration.
当我删除 user
时,所有 companies
都必须删除,当我删除 company
时,所有 users
都必须删除。
我做错了什么?
User.php
Schema::create('users', function (Blueprint $table) {
$table->engine = "InnoDB";
$table->bigIncrements('id');
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
Companies.php
Schema::create('companies', function (Blueprint $table) {
$table->engine = "InnoDB";
$table->increments('id')->unsigned();
$table->string('companyname');
$table->string('address');
$table->integer('housenumber');
$table->string('postalcode');
$table->string('city');
$table->string('province');
$table->string('email');
$table->string('phonenumber');
$table->timestamps();
});
CreateUserCompanyPivotTable.php
Schema::create('user_company', function (Blueprint $table) {
$table->engine = "InnoDB";
$table->integer('user_id')->unsigned();
$table->integer('company_id')->unsigned();
});
Schema::table('user_company', function (Blueprint $table) {
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->foreign('company_id')->references('id')->on('companies')->onDelete('cascade');
});
错误:
Illuminate\Database\QueryException : SQLSTATE[HY000]: General error: 1215
Cannot add foreign key constraint (SQL: alter table `user_company` add constraint `user_company_user_id_foreign` foreign key (`user_id`) references `users` (`id`) on delete cascade)
迁移失败,因为 user_company
中 user_id
的数据类型与 users
中的 id
不匹配。
您在 users
table 中使用了 bigIncrement()
,它创建了一个类型为 UNSIGNED BIGINT
的 auto_increment
字段。
在 user_company
table 中,您使用 integer()->unsigned()
创建 user_id
,这会创建一个 auto_increment
字段,类型为 UNSIGNED INT
。
MySQL 创建外键时需要两个字段类型相同。
要解决此问题,您应该用 bigInteger()->unsigned()
创建 user_id
。
Schema::create('user_company', function (Blueprint $table) {
$table->engine = "InnoDB";
$table->bigInteger('user_id')->unsigned();
$table->bigInteger('company_id')->unsigned();
// You don't need separate schema code to create foreign
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->foreign('company_id')->references('id')->on('companies')->onDelete('cascade');
});