I currently have two tables Symbols
and Prices
. I am currently creating a migration for Prices
and adding a FK constraint to Prices.symbol_id
which references Symbols.id
. This is the Prices
migration:
$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');
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用作外键的数据类型
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.