[英]Migration: Cannot add foreign key constraint
我正在嘗試在 Laravel 中創建外鍵,但是當我使用artisan
遷移我的表時,我拋出了以下錯誤:
[Illuminate\Database\QueryException]
SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL
: alter table `priorities` add constraint priorities_user_id_foreign foreign
key (`user_id`) references `users` (`id`))
我的遷移代碼如下:
優先級遷移文件
public function up()
{
//
Schema::create('priorities', function($table) {
$table->increments('id', true);
$table->integer('user_id');
$table->foreign('user_id')->references('id')->on('users');
$table->string('priority_name');
$table->smallInteger('rank');
$table->text('class');
$table->timestamps('timecreated');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
//
Schema::drop('priorities');
}
用戶遷移文件
public function up()
{
//
Schema::table('users', function($table)
{
$table->create();
$table->increments('id');
$table->string('email');
$table->string('first_name');
$table->string('password');
$table->string('email_code');
$table->string('time_created');
$table->string('ip');
$table->string('confirmed');
$table->string('user_role');
$table->string('salt');
$table->string('last_login');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
//
Schemea::drop('users');
}
關於我做錯了什么的任何想法,我想現在就得到這個,因為我有很多表需要創建,例如用戶、客戶、項目、任務、狀態、優先級、類型、團隊。 理想情況下,我想創建使用外鍵保存這些數據的表,即clients_project
和project_tasks
等。
希望有人可以幫助我開始。
分兩步添加,也可以不簽名:
public function up()
{
Schema::create('priorities', function($table) {
$table->increments('id', true);
$table->integer('user_id')->unsigned();
$table->string('priority_name');
$table->smallInteger('rank');
$table->text('class');
$table->timestamps('timecreated');
});
Schema::table('priorities', function($table) {
$table->foreign('user_id')->references('id')->on('users');
});
}
問題已經回答,但希望這可能對其他人有所幫助。
這個錯誤發生在我身上,因為我首先創建了帶有外鍵的遷移表,然后該鍵作為其原始表中的主鍵存在。 遷移按照它們創建的順序執行,如運行migrate:make
后生成的文件名所示。 例如2014_05_10_165709_create_student_table.php
。
解決方案是將帶有外鍵的文件重命名為比帶有主鍵的文件更早的時間,如此處推薦的那樣: http : //forumsarchive.laravel.io/viewtopic.php?id=10246
我想我還必須添加$table->engine = 'InnoDB';
從 Laravel 5.8 開始,遷移存根默認在 ID 列上使用 bigIncrements 方法。 以前,ID 列是使用增量方法創建的。
這不會影響您項目中的任何現有代碼; 但是,請注意外鍵列的類型必須相同。 因此,使用 increments 方法創建的列不能引用使用 bigIncrements 方法創建的列。
假設您正在構建一個簡單的基於角色的應用程序,您需要引用PIVOT表"role_user"中的user_id 。
2019_05_05_112458_create_users_table.php
// ...
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('full_name');
$table->string('email');
$table->timestamps();
});
}
2019_05_05_120634_create_role_user_pivot_table.php
// ...
public function up()
{
Schema::create('role_user', function (Blueprint $table) {
// this line throw QueryException "SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint..."
// $table->integer('user_id')->unsigned()->index();
$table->bigInteger('user_id')->unsigned()->index(); // this is working
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
});
}
如您所見,注釋行將拋出查詢異常,因為如升級說明中所述,外鍵列必須為相同類型,因此您需要將前鍵(在本例中為user_id )更改為BIGINTEGER在用戶表ROLE_USER表或改變bigIncrements方法增量法和使用數據透視表中注釋行,就看你了。
我希望我能夠向你澄清這個問題。
就我而言,問題是主表中已經有記錄,我強制新列不為 NULL。 因此,向新列添加 ->nullable() 就成功了。 在問題的例子中會是這樣的:
$table->integer('user_id')->unsigned()->nullable();
或者:
$table->unsignedInteger('user_id')->nullable();
希望這對某人有幫助!
在我的情況下,問題是users
表的自動生成的遷移正在設置
...
$table->bigIncrements('id');
...
所以我不得不改變列類型
$table->bigInteger('id');
使我的外鍵遷移工作。
這與 Laravel 5.8.2
在我的情況下,問題出在遷移時間上,在創建遷移時要小心,首先創建子遷移而不是基本遷移。 因為如果您首先創建具有外鍵的基本遷移,則會查找子表,並且不會有隨后拋出異常的表。
此外:
創建遷移時,它的開頭有一個時間戳。 假設你已經創建了一個遷移貓,所以它看起來像2015_08_19_075954_the_cats_time.php
並且它有這個代碼
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class TheCatsTime extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('cat', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->date('date_of_birth');
$table->integer('breed_id')->unsigned()->nullable();
});
Schema::table('cat', function($table) {
$table->foreign('breed_id')->references('id')->on('breed');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('cat');
}
}
創建基表后,您創建另一個遷移品種,即子表,它有自己的創建時間和日期戳。 代碼將如下所示:
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class BreedTime extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('breed', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('breed');
}
}
看起來這兩個表都是正確的,但是當您運行php artisan migrate 時。 它將拋出異常,因為遷移將首先在您的數據庫中創建基表,因為您首先創建了此遷移,並且我們的基表中具有外鍵約束,它將查找子表並且子表不存在,這可能是一個例外..
所以:
首先創建子表遷移。
創建子遷移后創建基表遷移。
php工匠遷移。
完成它會起作用
在 laravel 5.8 中, users_table 使用bigIncrements('id')
數據類型作為主鍵。 因此,當您想引用外鍵約束時,您的user_id
列需要是unsignedBigInteger('user_id')
類型。
我在 Laravel 5.8 上遇到了這個問題,我修復了這段代碼,如Laravel 文檔中所示,我在添加外鍵的地方。
$table->unsignedBigInteger('user_id');
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
然后我運行$ php artisan migrate:refresh
由於這種語法相當冗長,Laravel 提供了額外的、更簡潔的方法,這些方法使用約定來提供更好的開發人員體驗。 上面的例子可以這樣寫:
Schema::table('posts', function (Blueprint $table) {
$table->foreignId('user_id')->constrained()->onDelete('cascade');
});
就我而言,我只是更改手動執行遷移的順序,因此首先創建表用戶。
在文件夾 database/migrations/ 中,您的遷移文件名具有以下格式:year_month_day_hhmmss_create_XXXX_table.php
只需重命名創建用戶文件,以便您的表優先級表的創建日期設置為晚於用戶日期(即使晚一秒就足夠了)
我在使用Laravel 5.8時遇到了同樣的問題。 在仔細查看 laravel 文檔之后,此外還有遷移和 bigIncrements 。 我解決它的方法是將主鍵"$table->bigIncrements('id')" 添加到與表"users"及其關聯相關的每個表中,在我的例子中是表"role" 。 最后,我有"$table->unsignedBigInteger"用於將角色關聯到用戶(多對多),即表"role_user" 。
1. Users table
Schema::create('users', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
2. Roles Table
Schema::create('roles', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name')->unique();
$table->string('display_name')->nullable();
$table->string('description')->nullable();
$table->timestamps();
});
3. Table role_user
Schema::create('role_user', function (Blueprint $table) {
$table->unsignedBigInteger('user_id');
$table->unsignedBigInteger('role_id');
$table->foreign('user_id')->references('id')->on('users')
->onUpdate('cascade')->onDelete('cascade');
$table->foreign('role_id')->references('id')->on('roles')
->onUpdate('cascade')->onDelete('cascade');
$table->primary(['user_id', 'role_id']);
});
這個錯誤發生在我身上,因為 - 雖然我試圖創建的表是 InnoDB - 我試圖將它關聯到的外部表是 MyISAM 表!
使用 Laravel 5.3 也有同樣的問題。
解決方案是使用unsignedInteger而不是integer('name')->unsigned() 。
所以這就是有效的
$table->unsignedInt('column_name');
$table->foreign('column_name')->references('id')->on('table_name');
這樣做的原因是當使用integer('name')->unsigned時,表中創建的列的長度為 11,但使用unsigedInteger('name') 時,該列的長度為 10。
長度 10 是使用 Laravel 時主鍵的長度,因此列長度匹配。
我們不能添加關系,除非相關表被創建。 Laravel 按遷移文件的日期運行遷移順序。 因此,如果您想與第二個遷移文件中存在的表創建關系,它將失敗。
我遇到了同樣的問題,所以我最后又創建了一個遷移文件來指定所有關系。
Schema::table('properties', function(Blueprint $table) {
$table->foreign('user')->references('id')->on('users')->onDelete('cascade');
$table->foreign('area')->references('id')->on('areas')->onDelete('cascade');
$table->foreign('city')->references('id')->on('cities')->onDelete('cascade');
$table->foreign('type')->references('id')->on('property_types')->onDelete('cascade');
});
Schema::table('areas', function(Blueprint $table) {
$table->foreign('city_id')->references('id')->on('cities')->onDelete('cascade');
});
你應該這樣寫
public function up()
{
Schema::create('transactions', function (Blueprint $table) {
$table->bigIncrements('id');
$table->float('amount', 11, 2);
$table->enum('transaction type', ['debit', 'credit']);
$table->bigInteger('customer_id')->unsigned();
$table->timestamps();
});
Schema::table('transactions', function($table) {
$table->foreign('customer_id')
->references('id')->on('customers')
->onDelete('cascade');
});
}
外鍵字段應該是unsigned ,希望它有幫助!!
請注意:當 Laravel 使用
$table->increments('id');
這是大多數遷移的標准,這將設置一個無符號整數字段。 因此,當從另一個表對該字段進行外部引用時,請確保在引用表中將該字段設置為 UnsignedInteger 而不是(我認為是)UnsignedBigInteger 字段。
例如:在遷移文件 2018_12_12_123456_create_users_table.php 中:
Schema::create('users', function (Blueprint $table){
$table->increments('id');
$table->string('name');
$table->timestamps();
然后在遷移文件 2018_12_12_18000000_create_permissions_table.php 中,將外部引用設置回用戶:
Schema::create('permissions', function (Blueprint $table){
$table->increments('id');
$table->UnsignedInteger('user_id'); // UnsignedInteger = "increments" in users table
$table->boolean('admin');
$table->boolean('enabled');
$table->timestamps();
// set up relationship
$table->foreign('user_id')->reference('id')->on('users')->onDelete('cascade');
}
為了在 Laravel 中添加外鍵約束,以下對我有用:
創建列作為外鍵,如下所示:
$table->integer('column_name')->unsigned();
在(1)之后立即添加約束線即
$table->integer('column_name')->unsigned();\n $table->foreign('column_name')->references('pk_of_other_table')->on('other_table');
我知道那是一個老問題,但請確保您使用的參考文獻是否定義了正確的支持引擎。 為兩個表設置 innodb 引擎,並為引用列設置相同的數據類型
$table->engine = 'InnoDB';
我注意到的一件事是,如果表使用不同的引擎而不是外鍵約束不起作用。
例如,如果一張表使用:
$table->engine = 'InnoDB';
而其他用途
$table->engine = 'MyISAM';
會產生一個錯誤:
SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint
您可以通過在表創建結束時添加 InnoDB 來解決此問題,如下所示:
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedInteger('business_unit_id')->nullable();
$table->string('name', 100);
$table->foreign('business_unit_id')
->references('id')
->on('business_units')
->onDelete('cascade');
$table->timestamps();
$table->softDeletes();
$table->engine = 'InnoDB'; # <=== see this line
});
}
在最初的問題幾年后,使用 laravel 5.1,我遇到了同樣的錯誤,因為我的遷移是用所有相同的日期代碼由計算機生成的。 我瀏覽了所有建議的解決方案,然后重構以找到錯誤源。
在關注 laracasts 和閱讀這些帖子時,我相信正確的答案類似於 Vickies 的答案,除了您不需要添加單獨的架構調用。 您不需要將表設置為 Innodb,我假設 laravel 現在正在這樣做。
遷移只需要正確計時,這意味着您將在文件名中修改(稍后)需要外鍵的表的日期代碼。 或者或另外,降低不需要外鍵的表的日期代碼。
修改日期代碼的優點是您的遷移代碼更易於閱讀和維護。
到目前為止,我的代碼正在通過調整時間碼來推遲需要外鍵的遷移。
但是我確實有數百張表,所以最后我只有一張外鍵表。 只是為了讓事情順利進行。 我假設我會將它們拉入正確的文件並在我測試它們時修改日期代碼。
舉個例子:文件 2016_01_18_999999_create_product_options_table。 這個需要創建 products 表。 查看文件名。
public function up()
{
Schema::create('product_options', function (Blueprint $table) {
$table->increments('id');
$table->integer('product_attribute_id')->unsigned()->index();
$table->integer('product_id')->unsigned()->index();
$table->string('value', 40)->default('');
$table->timestamps();
//$table->foreign('product_id')->references('id')->on('products');
$table->foreign('product_attribute_id')->references('id')->on('product_attributes');
$table->foreign('product_id')->references('id')->on('products');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('product_options');
}
產品表:這需要先遷移。 2015_01_18_000000_create_products_table
public function up()
{
Schema::create('products', function (Blueprint $table) {
$table->increments('id');
$table->string('style_number', 64)->default('');
$table->string('title')->default('');
$table->text('overview')->nullable();
$table->text('description')->nullable();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('products');
}
最后是我臨時用來解決問題的文件,當我為我命名為 9999_99_99_999999_create_foreign_keys.php 的模型編寫測試時,我將重構該文件。 這些鍵在我拔出時已被注釋,但您明白了。
public function up()
{
// Schema::table('product_skus', function ($table) {
// $table->foreign('product_id')->references('id')->on('products')->onDelete('cascade');
// });
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
// Schema::table('product_skus', function ($table)
// {
// $table->dropForeign('product_skus_product_id_foreign');
// });
很簡單 !!!
如果您第一次創建'priorities'
遷移文件,Laravel 首先運行'priorities'
而'users'
表不存在。
它如何添加與不存在的表的關系!。
解決方案:從'priorities'
表中提取外鍵代碼。 你的遷移文件應該是這樣的:
並添加到一個新的遷移文件,這里它的名稱是create_prioritiesForeignKey_table
並添加以下代碼:
public function up()
{
Schema::table('priorities', function (Blueprint $table) {
$table->foreign('user_id')
->references('id')
->on('users');
});
}
確保您的前列超過前鍵列的范圍
我的意思是你的外鍵(在第二個表中)必須與你的 ponter 主鍵(在第一個表中)相同類型
您的指針主鍵必須是添加無符號方法,讓我展示一下:
在您的第一個遷移表上:
$table->increments('column_name'); //is INTEGER and UNSIGNED
在您的第二個遷移表上:
$table->integer('column_forein_name')->unsigned(); //this must be INTEGER and UNSIGNED
$table->foreign('column_forein_name')->references('column_name')->on('first_table_name');
另一個看到差異的例子
在您的第一個遷移表上:
$table->mediumIncrements('column_name'); //is MEDIUM-INTEGER and UNSIGNED
在您的第二個遷移表上:
$table->mediumInteger('column_forein_name')->unsigned(); //this must be MEDIUM-INTEGER and UNSIGNED
$table->foreign('column_forein_name')->references('column_name')->on('first_table_name');
如果上述解決方案都不適用於新手,請檢查兩個 ID 是否具有相同的類型:都是integer
或都是bigInteger
,......你可以有這樣的東西:
主表(例如用戶)
$table->bigIncrements('id');
子表(例如優先級)
$table->unsignedInteger('user_id');
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
此查詢將失敗,因為users.id
是一個BIG INTEGER
而priorities.user_id
是一個INTEGER
。
在這種情況下,正確的查詢如下:
$table->unsignedBigInteger('user_id');
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
也可能是你創建遷移的順序。 如果你先創建優先級表,然后創建用戶表,那就錯了。 因為第一次遷移尋找用戶表。 因此,您必須更改遷移順序
app/database/migrations
目錄
對我來說,問題是舊表使用的是 MyISAM 而不是 InnoDB。 這修復了它
$tables = [
'table_1',
'table_2'
];
foreach ($tables as $table) {
\DB::statement('ALTER TABLE ' . $table . ' ENGINE = InnoDB');
}
2021 年 4 月 20 日
在 Laravel 8 中,我遇到了這個問題。 如果您不使用nullable()
則可能會發生此錯誤。
$table->bigInteger('user_id')->nullable()->unsigned()->index();
$table->foreign('user_id')->references('id')->on('users')->onUpdate('cascade')->onDelete('set null');
就我而言,我在字符串user_id
列上引用整數id
列。 我變了:
$table->string('user_id')
到:
$table->integer('user_id')->unsigned();
希望它可以幫助某人!
要點是外來方法使用ALTER_TABLE
將預先存在的字段變成外鍵。 因此,您必須在應用外鍵之前定義表類型。 但是,它不必在單獨的Schema::
調用中。 您可以在 create 中執行這兩項操作,如下所示:
public function up()
{
Schema::create('priorities', function($table) {
$table->increments('id', true);
$table->integer('user_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users');
$table->string('priority_name');
$table->smallInteger('rank');
$table->text('class');
$table->timestamps('timecreated');
});
}
另請注意,將user_id
的類型設置為 unsigned 以匹配外鍵。
您可以直接在整數列中傳遞布爾參數,說明它是否應該是無符號的。 在 laravel 5.4 以下代碼解決了我的問題。
$table->integer('user_id', false, true);
這里第二個參數false表示它不應該是自動遞增的,第三個參數true表示它應該是無符號的。 您可以將外鍵約束保留在同一遷移中或將其分開。 它適用於兩者。
在我的情況下,直到我運行命令它才起作用
composer dump-autoload
這樣你就可以將外鍵留在創建架構中
public function up()
{
//
Schema::create('priorities', function($table) {
$table->increments('id', true);
$table->integer('user_id');
$table->foreign('user_id')->references('id')->on('users');
$table->string('priority_name');
$table->smallInteger('rank');
$table->text('class');
$table->timestamps('timecreated');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
//
Schema::drop('priorities');
}
對我來說,我的子表引用的表列沒有編入索引。
Schema::create('schools', function (Blueprint $table) {
$table->integer('dcid')->index()->unque();
$table->integer('school_number')->index(); // The important thing is that this is indexed
$table->string('name');
$table->string('abbreviation');
$table->integer('high_grade');
$table->integer('low_grade');
$table->timestamps();
$table->primary('dcid');
});
Schema::create('students', function (Blueprint $table) {
$table->increments('id');
$table->integer('dcid')->index()->unique()->nullable();
$table->unsignedInteger('student_number')->nullable();
$table->integer('schoolid')->nullable();
$table->foreign('schoolid')->references('school_number')->on('schools')->onDelete('set null');
// ...
});
忽略可怕的命名,它來自另一個設計非常糟糕的系統。
我試圖在Laravel中創建外鍵,但是當我使用artisan
遷移表時,拋出了以下錯誤:
[Illuminate\Database\QueryException]
SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL
: alter table `priorities` add constraint priorities_user_id_foreign foreign
key (`user_id`) references `users` (`id`))
我的遷移代碼是這樣的:
優先級遷移文件
public function up()
{
//
Schema::create('priorities', function($table) {
$table->increments('id', true);
$table->integer('user_id');
$table->foreign('user_id')->references('id')->on('users');
$table->string('priority_name');
$table->smallInteger('rank');
$table->text('class');
$table->timestamps('timecreated');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
//
Schema::drop('priorities');
}
用戶遷移文件
public function up()
{
//
Schema::table('users', function($table)
{
$table->create();
$table->increments('id');
$table->string('email');
$table->string('first_name');
$table->string('password');
$table->string('email_code');
$table->string('time_created');
$table->string('ip');
$table->string('confirmed');
$table->string('user_role');
$table->string('salt');
$table->string('last_login');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
//
Schemea::drop('users');
}
關於我做錯了什么的任何想法,我想現在就解決,因為我需要創建很多表,例如用戶,客戶,項目,任務,狀態,優先級,類型,團隊。 理想情況下,我想創建使用外鍵保存此數據的表,即clients_project
和project_tasks
等。
希望有人可以幫助我入門。
(學習英語,抱歉)我在我的項目中嘗試使用“foreignId”並工作。 在您的代碼中,只需刪除列 user_id 並在引用中添加 foreignId :
public function up()
{
Schema::create('priorities', function($table) {
$table->increments('id', true);
$table->foreignId('user_id')->references('id')->on('users');
$table->string('priority_name');
$table->smallInteger('rank');
$table->text('class');
$table->timestamps('timecreated');
});
}
重要提示:在這種情況下,首先創建沒有外鍵的表,即“用戶”表
另一個原因可能是數據庫引擎類型。 Laravel 默認使用 MyISAM 引擎。 所以如果你需要強制執行外鍵引用檢查,你應該使用 InnoDB 作為你的引擎。
這個設置在你的config/database.php
文件中。 將引擎更改為'engine' => 'InnoDB'
此外,如果您嘗試對現有表執行此操作,請使用以下方法更改引擎
DB::statement("ALTER TABLE `{$tableName}` ENGINE = 'InnoDB'");
我認為這里的答案缺少一件事,如果我錯了,請更正我,但是外鍵需要在數據透視表上建立索引。 至少在mysql中似乎是這樣。
public function up()
{
Schema::create('image_post', function (Blueprint $table) {
$table->engine = 'InnoDB';
$table->increments('id');
$table->integer('image_id')->unsigned()->index();
$table->integer('post_id')->unsigned()->index();
$table->timestamps();
});
Schema::table('image_post', function($table) {
$table->foreign('image_id')->references('id')->on('image')->onDelete('cascade');
$table->foreign('post_id')->references('id')->on('post')->onDelete('cascade');
});
}
制作數據透視表時,我在Laravel 5上遇到了相同的錯誤,而我的問題是我沒有
->onDelete('cascade');
我認為:引用鍵必須是“索引”。 例如:(下)
public function up()
{
Schema::create('clicks', function (Blueprint $table) {
$table->increments('id');
$table->string('viewer_id');
$table->integer('link_id')->index()->unsigned();
$table->string('time');
$table->timestamps();
});
Schema::table('clicks', function($table) {
$table->foreign('link_id')->references('id')->on('links')->onDelete('cascade');
});
}
祝好運。
請先檢查列的數據類型。 如果第一個表 id 類型是 bigint,那么在添加外鍵時檢查其他表中的類型應該是 bigint。
在自動創建的 create_users_table 遷移中更改$table->id();
到$table->increments('id');
. 為我做的伎倆,希望這有幫助!
捷徑解決方案:
//內部架構::創建
$table->foreign('user_id')->bigInteger('user_id')->unsigned()->index();
//內部架構::表
$table->foreign('users_id')->references('id')->on('users')->onUpdate('cascade')->onDelete('cascade');
//處理你的代碼。
public function up()
{
//
Schema::create('priorities', function($table) {
$table->increments('id', true);
$table->integer('user_id');
$table->foreign('user_id')->bigInteger('user_id')->unsigned()->index();
$table->string('priority_name');
$table->smallInteger('rank');
$table->text('class');
$table->timestamps('timecreated');
});
Schema::table('administrador', function($table) {
$table->foreign('users_id')->references('id')->on('users')
->onUpdate('cascade')
->onDelete('cascade');
;
});
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.