MySQL ALTER TABLE 创建:“完整性约束违规:1452”
下面的代码在执行时会产生以下错误:
致命错误:未捕获异常“PDOException”,消息为“SQLSTATE[23000]:完整性约束违规:1452 无法添加或更新子行:外键约束失败
$db->executeUpdate('CREATE TABLE submission_statuses (name VARCHAR(255) NOT NULL,
weight INT NOT NULL, PRIMARY KEY(name)) ENGINE = InnoDB');
$db->executeUpdate('ALTER TABLE submissions ADD CONSTRAINT IDX_4
FOREIGN KEY (status) REFERENCES submission_statuses(name)');
$db->executeUpdate('CREATE INDEX IDX_4 ON submissions (status)');
这两个表都使用InnoDB 引擎。
The code below, when executed produces the following error:
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails
$db->executeUpdate('CREATE TABLE submission_statuses (name VARCHAR(255) NOT NULL,
weight INT NOT NULL, PRIMARY KEY(name)) ENGINE = InnoDB');
$db->executeUpdate('ALTER TABLE submissions ADD CONSTRAINT IDX_4
FOREIGN KEY (status) REFERENCES submission_statuses(name)');
$db->executeUpdate('CREATE INDEX IDX_4 ON submissions (status)');
The tables both use the InnoDB engine.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
表
submissions
在status
列中包含新创建的submission_statuses
表中不存在的值。在创建约束之前必须填充submission_statuses
表。The table
submissions
contains a value in thestatus
column that does not exist in the newly createdsubmission_statuses
table. Thesubmission_statuses
table must be populated before the constraint is created.