0

I have this error when inserting values into an association table during transaction:

Cannot add or update a child row: a foreign key constraint fails (dev.genre, CONSTRAINT fk_Genre_EntityType1 FOREIGN KEY (idEntityType) REFERENCES entitytype (idEntityType) ON DELETE NO ACTION ON UPDATE NO ACTION)

Here is the part of the schema that describes the tables used:

enter image description here

and here is the create statement of the genre table:

-- -----------------------------------------------------
-- Table `dev`.`Genre`
-- -----------------------------------------------------

CREATE  TABLE IF NOT EXISTS `dev`.`Genre` (
  `idGenre` INT NOT NULL ,
  `Name` VARCHAR(45) NULL COMMENT 'musique, spectacle, expo' ,
  `idEntityType` INT NOT NULL ,
  `idStyle` INT NOT NULL ,
  PRIMARY KEY (`idGenre`, `idEntityType`, `idStyle`) ,
  INDEX `fk_Genre_EntityType1_idx` (`idEntityType` ASC) ,
  INDEX `fk_Genre_Style1_idx` (`idStyle` ASC) ,
  CONSTRAINT `fk_Genre_EntityType1`
    FOREIGN KEY (`idEntityType` )
    REFERENCES `dev`.`EntityType` (`idEntityType` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_Genre_Style1`
    FOREIGN KEY (`idStyle` )
    REFERENCES `dev`.`Style` (`idStyle` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;

To resume, the Genre tables references EntityType and Style, that's all.

The error occurs when I try to create a new Style and then add an association in the Genre table.

Everything is within a transaction, and what I do is:

  • create the new style in the Style table
  • get the id of the created style
  • insert an association in the genre table, and that's when I get the error.

I've searched quite a while on the web, but the only thing I found was this SO post: In MySQL, can I defer referential integrity checks until commit

I'm not sure this is what it is about here, because the error happens on a table that hadn't changed during the transaction (EntityType). Or am I missing something?

Can someone explain me the reason why I have this error please? (I'm stuck here)

Also, if it really have something to do with the SO post I mentioned earlier, is there a "clean" way of doing that kind of inserts without writing my own rollback mechanism?

Thanks for your answers

EDIT

the first query to insert a new style is:

CREATE PROCEDURE `Entity_CreateStyle`
(
    IN p_name varchar(45),
    OUT op_idStyle int(11)
)
BEGIN
    insert into Style(idParentStyle, Name, IsValidated)
    values(null, p_name, 0);

    SET op_idStyle = LAST_INSERT_ID();
END

the next one, that produces the error:

CREATE PROCEDURE `Entity_AssociateStyleWithEntityType`
(
    IN p_idGenre int(11),
    IN p_Name varchar(45),
    IN p_idEntityType int(11),
    IN p_idStyle int(11)
)
BEGIN
    insert into Genre(idGenre, Name, idEntityType, idStyle)
    values(p_idGenre, p_Name, p_idEntityType, p_idStyle);
END

both are actually stored procedures that we call from C# using MySQL.Net connector

the p_idEntityType comes from the model (MVC) and I checked it's value is correct and exists in EntityType table.

Community
  • 1
  • 1
ppetrov
  • 3,077
  • 2
  • 15
  • 27
  • You are inserting a value in the column `idEntityType` that has no corresponding PK value in the `EntityType` table. –  Apr 23 '13 at 22:09
  • Well that's what I checked first, and I'm sure there is a corresponding PK in `EntityType` table, that's why I don't understand the error... – ppetrov Apr 24 '13 at 00:41
  • Perhaps I didn't add enough elements to my question, but I don't see which ones. It's a C# mvc4 website, and we're using the standard MySQL connector for transactions – ppetrov Apr 24 '13 at 00:46

1 Answers1

1

The error message is clear: entitytype is referenced by genre. This means that ALL rows in entitytype must have a match in genre, or can't be inserted. There is a match when genre.entitytype = entitytype.entitytype.

Federico Razzoli
  • 4,901
  • 1
  • 19
  • 21
  • See my comment on the question, there is a match between `Genre.idEntityType` and `EntityType.idEntityType`.... kind of weird... – ppetrov Apr 24 '13 at 00:42
  • Mmmh... but there is clearly something wrong. Please, post the query which produces the error. – Federico Razzoli Apr 24 '13 at 00:51
  • Yep there's clearly something wrong you're right! I still don't see what though... posted the queries – ppetrov Apr 24 '13 at 00:56
  • You posted the queries, but not the procedures :) – Federico Razzoli Apr 24 '13 at 01:05
  • If you don't want to post the procedures, please add "SELECT p_idEntityType;" just before the insert, and then call the procedure. You'll probably see that the value is not what you expect. – Federico Razzoli Apr 24 '13 at 01:06
  • I don't care about posting the procedures, done. BTW the `idEntityType` field I provide exists in the `EntityType` table, this also was one of the first things I checked – ppetrov Apr 24 '13 at 01:12
  • Ok, p_idEntityType is INT. Is it big enough for your value? And what about entitytype.idEntityType? Is it int too? – Federico Razzoli Apr 24 '13 at 01:24
  • yep int too, same type `int(11)`, and surely big enough (last value was `15`), thanks for your help btw, you spent quite much time on this now – ppetrov Apr 24 '13 at 01:33
  • But it didn't help, until now. Try adding this select before the insert: "SELECT p_idGenre, p_Name, p_idEntityType, p_idStyle;". Then, try executing the query with those exact values, but directly from the command line, not from a procedure. See if it works. If it does, there is a bug in MySQL (in that case, reporting a bug is a good practice of course). – Federico Razzoli Apr 24 '13 at 01:44
  • And, I forgot: execute this: SET @@session.SQL_MODE = 'ERROR_FOR_DIVISION_BY_ZERO,NO_ZERO_DATE,NO_ZERO_IN_DATE,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION,ONLY_FULL_GROUP_BY,STRICT_ALL_TABLES,STRICT_TRANS_TABLES'; After this select, please re-create the procedure. And call it again. Sometimes, MySQL procedures debugging is made harder by "relaxed" settings (which are the default). – Federico Razzoli Apr 24 '13 at 01:47
  • Another thing I forgot. It has nothing to do with your problem I think, but did you realize that you are using signed ids? – Federico Razzoli Apr 24 '13 at 01:52
  • well I didn't think about this, I'm not so good with SQL, is it a problem? (btw let's move it to chat, there's a lot of comments now) – ppetrov Apr 24 '13 at 02:02
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/28774/discussion-between-ppetrov-and-federico) – ppetrov Apr 24 '13 at 02:02
  • Actually there was a match, but the parameters order wasn't right in the C# method that called the procedure (idStyle was passed into idEntityType.....), so it didn't match at the end of the line.... – ppetrov Apr 24 '13 at 12:26