[英]Cannot add foreign key constraint in Laravel Migrations
I currently have two tables Symbols
and Prices
. 我目前有两个表Symbols
和Prices
。 I am currently creating a migration for Prices
and adding a FK constraint to Prices.symbol_id
which references Symbols.id
. 我目前正在为Prices
创建迁移,并向Prices.symbol_id
添加FK约束,该约束引用Symbols.id
。 This is the Prices
migration: 这是Prices
迁移:
$table->increments('id');
...
...
$table->integer('symbol_id');
$table->foreign('symbol_id')
->references('id')->on('Symbols')
->onDelete('cascade’);
Symbols.id
is simply a $table->increments('id');
Symbols.id
只是$table->increments('id');
Symbols.id
$table->increments('id');
However, when I run the migration, this is what happens: 但是,当我运行迁移时,会发生以下情况:
[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
Any ideas why? 有什么想法吗?
make a new migration 进行新的迁移
php artisan make:migration update_prices_table
The schema: 模式:
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.