1452无法添加或更新子行

1452无法添加或更新子行

本文介绍了SQLSTATE [23000]:违反完整性约束:1452无法添加或更新子行(Laravel 6)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在做什么错了?

环境:Laravel 6,宅基(本地),Windows 10

Environment:Laravel 6, Homestead (Local), Windows 10

创建外部表(迁移):

Schema::create('external', function (Blueprint $table) {
                $table->increments('id')->unsigned();
                $table->foreign('id')->references('order_id')->on('order');
            });

创建订单表(迁移):

Schema::create('order', function (Blueprint $table) {
    $table->increments('id');
    $table->unsignedInteger('order_id')->index();

External.php(模型):

External.php (model):

class External extends Model
    public function orders()
    {
        return $this->hasMany(Order::class);
    }
}

Order.php(模型):

Order.php (Model):

public function external()
{
    return $this->belongsTo(External::class);
}

错误消息:

推荐答案

该错误是常见错误,很容易解释.您的Laravel代码正在生成一个(或多个)插入,该插入指向 order 表中不存在的记录.尝试运行以下查询:

The error is a common one, and easy to explain. Your Laravel code is generating one (or more) insert which is referring to a record in the order table which does not exist. Try running the following query:

SELECT o.*
FROM [order] o
WHERE order_id = <external.id value here>

如果返回空结果集,则表示您的插入内容引用的是 order 中不存在的数据.

If you get back an empty result set, then it means your insert is referring to data in order which does not exist.

这篇关于SQLSTATE [23000]:违反完整性约束:1452无法添加或更新子行(Laravel 6)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 04:09