如何解决Laravel外键错误?

4
我创建了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');
});
1个回答

3
这意味着我们传递给table2的值在table1中作为父项不存在。 在你的第二个表中进行更改。
$table->integer('cryptocurrency_id');

为了

$table->unsignedBigInteger('cryptocurrency_id');

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接