小编Ale*_*kov的帖子

Laravel - 使用数据在现有表上添加外键

我有现有的objects数据表.现在我需要holdings添加一个名为的新表,并添加从对象到holdings表的关系.在迁移文件中,我打印出来:

$table->foreign('holding_id')->references('id')->on('holdings')->onDelete("NO ACTION");
Run Code Online (Sandbox Code Playgroud)

尝试迁移时出现此错误

SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update 
a child row: a foreign key constraint fails (`kolomnaoffice`.`#sql-f10_126` 
CONSTRAINT `objects_holding_id_foreign` FOREIGN KEY (`holding_id`) 
REFERENCES `holdings` (`id`) ON DELETE NO ACTION) (SQL: alter table `objects` add constraint 
`objects_holding_id_foreign` foreign key (`holding_id`) references `holdings` 
(`id`) on delete NO ACTION)
Run Code Online (Sandbox Code Playgroud)

我有正确的数据库结构(都是InnoDB),字段存在并且具有正确的类型(int).唯一不同的是表objects填充了数据,表holdings是新的和空的.

mysql migration foreign-keys laravel laravel-5.4

7
推荐指数
2
解决办法
1万
查看次数

标签 统计

foreign-keys ×1

laravel ×1

laravel-5.4 ×1

migration ×1

mysql ×1