我目前有两张桌子Symbols
和Prices
。我目前正在为Prices
创建一个迁移,并向引用Prices.symbol_id
的Symbols.id
添加一个FK约束。这是Prices
迁移:
$table->increments('id');
...
...
$table->integer('symbol_id');
$table->foreign('symbol_id')
->references('id')->on('Symbols')
->onDelete('cascade’);
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作为外键的数据类型