1

I'm currently breaking my head on a 1452 error (using Symfony2 with doctrine).

Here is the case: I have 3 tables, Location, Concert and CD, and as expected a Concert take place at a Location, and a CD is related to a Concert, that's including some foreign keys.

Here is the SQL queries generated by doctrine to create the database:

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;

The fact is, there is no problem to insert Location, and a connected Concert, to satisfy the CD's foreign key:

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);

But then if I try to insert a CD:

INSERT INTO  CD (id ,concert ,number) VALUES (NULL ,  '1',  '1');

I got the famous error:

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)

So I've tried some hacks:

  1. Escape the creation of the table Location and the related key:

    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;
    

    That's working well.

  2. Delete the table Location after creation (and the related key):

    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;
    

    Same error.

  3. Escape foreign key controlling

    SET FOREIGN_KEY_CHECKS = 0;
    

    Same error.

The two last tries, make me think of some index issue on the link Concert-Location ? You understand, I am a bit lost.

Does anyone have already faced something similar ?

Thank a lot for knowledge,

Tom.

T3e
  • 539
  • 1
  • 6
  • 17
  • I've tried to skip the indexes and just use alone foreign keys. I am not successful, so my last idea was wrong. – T3e Jun 18 '12 at 18:49
  • Alright, I've tried on another MySQL server, and it's working. The only difference I can notice is that I am running MySQL 5.5.9 (which fails) and the successful try is on 5.1.44 – T3e Jun 19 '12 at 13:46

1 Answers1

0

So,

According to MySQL documentation, MySQL server since 5.5.9 on OSX, fails on insertion with foreign keys. This bug seems to come with case sensitivity.

There is many ways to fix it (this list is non-exhaustive, and some options may not be effective):

  • Use only lowercase tables names.
  • Try to set lower_case_table_names to O or 1 (but on OS X it's not always possible cause of system's case sensitivity).
  • Downgrade to MySQL server 5.5.8 (I've not tested upgrading).

Thanks to this thread.

Community
  • 1
  • 1
T3e
  • 539
  • 1
  • 6
  • 17