問題描述
誰能幫我解決這個(gè)問題?
Can anybody help me to solve this problem?
有 3 個(gè)表,有 2 個(gè)外鍵:
There are 3 tables with 2 foreign keys:
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('email')->unique();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
Schema::create('firms', function (Blueprint $table) {
$table->increments('id');
$table->string('title')->nullable();
$table->integer('user_id')->unsigned()->nullable();
$table->foreign('user_id')->references('id')->on('users');
$table->timestamps();
});
Schema::create('jobs', function (Blueprint $table) {
$table->increments('id');
$table->string('title')->nullable();
$table->integer('firm_id')->unsigned()->nullable();
$table->foreign('firm_id')->references('id')->on('firms');
$table->timestamps();
});
運(yùn)行遷移后出錯(cuò):
[IlluminateDatabaseQueryException]
SQLSTATE[HY000]: General error: 1005 Can't create table `job`.`#sql-5fc_a1`
(errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter ta
ble `firms` add constraint `firms_user_id_foreign` foreign key (`user_id`)
references `users` (`id`))
[PDOException]
SQLSTATE[HY000]: General error: 1005 Can't create table `job`.`#sql-5fc_a1`
(errno: 150 "Foreign key constraint is incorrectly formed")
推薦答案
如果是外鍵,引用字段和引用字段的數(shù)據(jù)類型必須完全相同.
In case of foreign keys, the referenced and referencing fields must have exactly the same data type.
您將users
和firms
中的id
字段創(chuàng)建為有符號 整數(shù).但是,您將兩個(gè)外鍵創(chuàng)建為無符號 整數(shù),因此鍵的創(chuàng)建失敗.
You create the id
fields in both users
and firms
as signed integers. However, you create both foreign keys as unsigned integers, therefore the creation of the keys fail.
您需要將 unsigned
子句添加到 id
字段定義中,或者從外鍵字段中刪除 unsigned
子句.
You need to either add the unsigned
clause to the id
field definitions, or remove the unsigned
clause from the foreign key fields.
這篇關(guān)于laravel errno 150 外鍵約束的格式不正確的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!