1
我已经创建了一个新的迁移,以将列添加到现有表并向现有表中添加外键。Laravel - 无法解决完整性约束违规
这是新列迁移:
Schema::table('events', function (Blueprint $table) {
$table->integer('category_id')->unsigned()->after('place_id');
$table->foreign('category_id')->references('id')->on('categories');
});
当我运行迁移命令我得到:
[Illuminate\Database\QueryException]
SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`meetmount`.`#sql-3c8_424`, CONSTRAINT `events_catego
ry_id_foreign` FOREIGN KEY (`category_id`) REFERENCES `categories` (`id`)) (SQL: alter table `events` add constraint events_category_id_foreign foreign key (`category_id`) r
eferences `categories` (`id`))
和
[PDOException]
SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`meetmount`.`#sql-3c8_424`, CONSTRAINT `events_catego
ry_id_foreign` FOREIGN KEY (`category_id`) REFERENCES `categories` (`id`))
我该如何解决?
谢谢,这个问题是不是空的父表 –