我创建了 2 个 Laravel 迁移,第一个运行完美,第二个给我一个错误:
SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails
我不确定我的外键出了什么问题,有人可以帮助我吗?
这是我的第一个架构:
Schema::create('cryptocurrencies', function (Blueprint $table) {
$table->id('id');
$table->string('name');
$table->string('symbol')->unique();
$table->string('slug');
$table->longtext('description');
});
这是我的第二个架构:
Schema::create('cryptocurrencies_quotes', function (Blueprint $table) {
$table->id('id');
$table->string('name');
$table->string('symbol');
$table->string('slug');
$table->integer('cryptocurrency_id');
$table->bigInteger('circulating_supply');
$table->bigInteger('total_supply');
$table->double('price');
$table->double('volume_24h');
$table->timestamps();
});
Schema::table('cryptocurrencies_quotes', function (Blueprint $table) {
$table->foreign('cryptocurrency_id')->references('id')->on('cryptocurrencies')->onUpdate('cascade')->onDelete('cascade');
});
这意味着我们传递给table2的值有foreign不存在于table1作为父级 在你的第二张桌子上改变
$table->integer('cryptocurrency_id');
对于
$table->unsignedBigInteger('cryptocurrency_id');