【发布时间】:2020-01-23 03:33:00
【问题描述】:
我做错了什么?
环境: Laravel 6,家园(本地),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(模型):
class External extends Model
public function orders()
{
return $this->hasMany(Order::class);
}
}
Order.php(型号):
public function external()
{
return $this->belongsTo(External::class);
}
错误信息:
SQLSTATE[23000]:完整性约束违规:1452 无法添加或更新子行:外键约束失败(
homestead.external,CONSTRAINTexternal_id_foreignFOREIGN KEY(id)参考order(order_id)) (SQL: 插入external(site_order_id,order_status,...
【问题讨论】: