我目前正在犯1452错误(使用带有学说的Symfony2)。
以下是这种情况: 我有3个表,位置,音乐会和CD,正如预期的那样,音乐会在一个位置发生,一张CD与音乐会有关,包括一些外键。
以下是doctrine生成的用于创建数据库的SQL查询:
CREATE TABLE CD (id INT AUTO_INCREMENT NOT NULL, concert INT NOT NULL, number INT NOT NULL, INDEX IDX_EB3C8BB0D57C02D2 (concert), PRIMARY KEY(id)) ENGINE = InnoDB;
CREATE TABLE Concert (id INT AUTO_INCREMENT NOT NULL, location INT NOT NULL, date DATETIME NOT NULL, name VARCHAR(255) DEFAULT NULL, INDEX IDX_1AC13B4E5E9E89CB (location), PRIMARY KEY(id)) ENGINE = InnoDB;
CREATE TABLE Location (id INT AUTO_INCREMENT NOT NULL, name VARCHAR(40) NOT NULL, city VARCHAR(40) NOT NULL, PRIMARY KEY(id)) ENGINE = InnoDB;
ALTER TABLE CD ADD CONSTRAINT FK_EB3C8BB0D57C02D2 FOREIGN KEY (concert) REFERENCES Concert(id) ON UPDATE CASCADE ON DELETE CASCADE;
ALTER TABLE Concert ADD CONSTRAINT FK_1AC13B4E5E9E89CB FOREIGN KEY (location) REFERENCES Location(id) ON UPDATE CASCADE ON DELETE CASCADE;
事实是,插入位置和连接的Concert以满足CD的外键没有问题:
INSERT INTO Location (id, name, city) VALUES (NULL, 'Church', 'Berlin');
INSERT INTO Concert (id, location, date, name) VALUES (NULL, '1', '2012-06-20 19:30:00', NULL);
但是如果我尝试插入CD:
INSERT INTO CD (id ,concert ,number) VALUES (NULL , '1', '1');
我得到了一个着名的错误:
Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`festival`.`cd`, CONSTRAINT `FK_EB3C8BB0D57C02D2` FOREIGN KEY (`concert`) REFERENCES `Concert` (`id`) ON DELETE CASCADE ON UPDATE CASCADE)
所以我尝试了一些黑客攻击:
转义表位置及相关密钥的创建:
CREATE TABLE CD (id INT AUTO_INCREMENT NOT NULL, concert INT NOT NULL, number INT NOT NULL, INDEX IDX_EB3C8BB0D57C02D2 (concert), PRIMARY KEY(id)) ENGINE = InnoDB;
CREATE TABLE Concert (id INT AUTO_INCREMENT NOT NULL, location INT NOT NULL, date DATETIME NOT NULL, name VARCHAR(255) DEFAULT NULL, INDEX IDX_1AC13B4E5E9E89CB (location), PRIMARY KEY(id)) ENGINE = InnoDB;
ALTER TABLE CD ADD CONSTRAINT FK_EB3C8BB0D57C02D2 FOREIGN KEY (concert) REFERENCES Concert(id) ON UPDATE CASCADE ON DELETE CASCADE;
运作良好。
创建后删除表格位置(和相关密钥):
CREATE TABLE CD (id INT AUTO_INCREMENT NOT NULL, concert INT NOT NULL, number INT NOT NULL, INDEX IDX_EB3C8BB0D57C02D2 (concert), PRIMARY KEY(id)) ENGINE = InnoDB;
CREATE TABLE Concert (id INT AUTO_INCREMENT NOT NULL, location INT NOT NULL, date DATETIME NOT NULL, name VARCHAR(255) DEFAULT NULL, INDEX IDX_1AC13B4E5E9E89CB (location), PRIMARY KEY(id)) ENGINE = InnoDB;
CREATE TABLE Location (id INT AUTO_INCREMENT NOT NULL, name VARCHAR(40) NOT NULL, city VARCHAR(40) NOT NULL, PRIMARY KEY(id)) ENGINE = InnoDB;
ALTER TABLE CD ADD CONSTRAINT FK_EB3C8BB0D57C02D2 FOREIGN KEY (concert) REFERENCES Concert(id) ON UPDATE CASCADE ON DELETE CASCADE;
ALTER TABLE Concert ADD CONSTRAINT FK_1AC13B4E5E9E89CB FOREIGN KEY (location) REFERENCES Location(id) ON UPDATE CASCADE ON DELETE CASCADE;
ALTER TABLE Concert DROP FOREIGN KEY FK_1AC13B4E5E9E89CB;
ALTER TABLE Concert DROP INDEX IDX_1AC13B4E5E9E89CB;
DROP TABLE Location;
同样的错误。
逃避外键控制
SET FOREIGN_KEY_CHECKS = 0;
同样的错误。
最后两次尝试,让我想起Concert-Location链接上的一些索引问题? 你理解,我有点失落。
有没有人遇到过类似的事情?
非常感谢知识,
汤姆。
答案 0 :(得分:0)
所以,
根据MySQL documentation,OSX上5.5.9以来的MySQL服务器在插入外键时失败。这个错误似乎与案例敏感性有关。
有很多方法可以修复它(此列表并非详尽无遗,有些选项可能无效):