[英]Cannot add foreign key constraint in Laravel Migrations
我目前有兩個表Symbols
和Prices
。 我目前正在為Prices
創建遷移,並向Prices.symbol_id
添加FK約束,該約束引用Symbols.id
。 這是Prices
遷移:
$table->increments('id');
...
...
$table->integer('symbol_id');
$table->foreign('symbol_id')
->references('id')->on('Symbols')
->onDelete('cascade’);
Symbols.id
只是$table->increments('id');
Symbols.id
$table->increments('id');
但是,當我運行遷移時,會發生以下情況:
[Illuminate\Database\QueryException]
SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL: alter
table `Prices` add constraint prices_symbol_id_foreign foreign key (`symbol_id`) re
ferences `Symbols` (`id`))
[PDOException]
SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint
有什么想法嗎?
進行新的遷移
php artisan make:migration update_prices_table
模式:
public function up()
{
Schema::table('Prices', function (Blueprint $table) {
$table->integer('symbol_id)->unsigned();
$table->foreign('symbol_id')
->references('id')->on('Symbols')
->onDelete('cascade’);
});
}
如果您的主鍵類型為bigIncrements ,請確保將bigInteger用作外鍵的數據類型
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.