[英]Migration Problem: Cannot add foreign key constraint in laravel
我試圖在Laravel 5.7中創建外鍵,但是當我使用工匠遷移表時,拋出了以下錯誤:
Illuminate \\ Database \\ QueryException:
SQLSTATE [HY000]:常規錯誤:1215無法添加外鍵約束(SQL:alter table
gateway_transactions
添加約束gateway_transactions_user_id_foreign
外鍵(user_id
)引用刪除CASCADE時的users
(id
)
我的遷移:
public function up()
{
Schema::create('gateway_transactions', function (Blueprint $table) {
$table->engine = "innoDB";
$table->unsignedBigInteger('id', true);
$table->integer('user_id')->unsigned();
$table->enum('provider', \Parsisolution\Gateway\GatewayManager::availableDrivers());
$table->decimal('amount', 15, 2);
$table->integer('order_id')->nullable();
$table->string('currency', 3)->nullable();
$table->string('ref_id', 100)->nullable();
$table->string('tracking_code', 50)->nullable();
$table->string('card_number', 50)->nullable();
$table->enum('status', \Parsisolution\Gateway\Transaction::availableStates())
->default(\Parsisolution\Gateway\Transaction::STATE_INIT);
$table->string('ip', 20)->nullable();
$table->json('extra')->nullable();
$table->timestamp('paid_at')->nullable();
$table->nullableTimestamps();
$table->softDeletes();
});
Schema::table('gateway_transactions', function(Blueprint $table) {
$table->foreign('user_id')->references('id')->on('users')->onDelete('CASCADE');
});
}
用戶遷移:
Schema::create(config('access.table_names.users'), function (Blueprint $table) {
$table->increments('id');
$table->uuid('uuid');
$table->string('first_name')->nullable();
$table->string('last_name')->nullable();
$table->string('email')->unique();
$table->string('avatar_type')->default('gravatar');
$table->string('avatar_location')->nullable();
$table->string('password')->nullable();
$table->timestamp('password_changed_at')->nullable();
$table->tinyInteger('active')->default(1)->unsigned();
$table->string('confirmation_code')->nullable();
$table->boolean('confirmed')->default(config('access.users.confirm_email') ? false : true);
$table->string('timezone')->nullable();
$table->text('National_Code')->nullable();
$table->char('phone_number', 11)->nullable()->unique();
$table->integer('phone_verify')->default(0);
$table->char('mobile_number', 11)->nullable()->unique();
$table->integer('mobile_verify')->default(0);
$table->text('state')->nullable();
$table->text('city')->nullable();
$table->text('address')->nullable();
$table->text('path')->nullable();
$table->char('postal_code', 10)->nullable();
$table->timestamp('last_login_at')->nullable();
$table->string('last_login_ip')->nullable();
$table->rememberToken();
$table->timestamps();
$table->softDeletes();
});
為此更改代碼:
public function up()
{
Schema::create('gateway_transactions', function (Blueprint $table) {
$table->engine = "innoDB";
$table->unsignedBigInteger('id', true);
$table->integer('user_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users')->onDelete('CASCADE');
$table->enum('provider', \Parsisolution\Gateway\GatewayManager::availableDrivers());
$table->decimal('amount', 15, 2);
$table->integer('order_id')->nullable();
$table->string('currency', 3)->nullable();
$table->string('ref_id', 100)->nullable();
$table->string('tracking_code', 50)->nullable();
$table->string('card_number', 50)->nullable();
$table->enum('status', \Parsisolution\Gateway\Transaction::availableStates())
->default(\Parsisolution\Gateway\Transaction::STATE_INIT);
$table->string('ip', 20)->nullable();
$table->json('extra')->nullable();
$table->timestamp('paid_at')->nullable();
$table->nullableTimestamps();
$table->softDeletes();
});
}
我認為-以下部分是我如何理解Laravel的工作方式。
由於要在第一個模式中創建表,因此必須將外鍵放入創建中。
問題在於,Laravel Migration將在應用代碼之前驗證一切正常,但是階段是函數up()。 因此,其中的所有內容都處於同一階段。 遷移認為您的gateway_transactions不存在,並且它在驗證一切正常時不存在。 該代碼將在何時存在。
外鍵列的類型必須與參考表完全相同。
如果用戶表的ID是一個大整數:
$table->bigIncrements('id');
您的外鍵必須是:
$table->unsignedBigInteger('user_id');
然后,遷移將正常工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.