MySQL Error 1215: Cannot add foreign key constraint

前端 未结 30 2816
予麋鹿
予麋鹿 2020-11-22 01:37

I am trying to forward engineer my new schema onto my db server, but I can\'t figure out why I am getting this error. I\'ve tried to search for the answer here, but everyth

相关标签:
30条回答
  • 2020-11-22 02:22

    I know i am VERY late to the party but i want to put it out here so that it is listed.

    As well as all of the above advice for making sure that fields are identically defined, and table types also have the same collation, make sure that you don't make the rookie mistake of trying to link fields where data in the CHILD field is not already in the PARENT field. If you have data that is in the CHILD field that you have not already entered in to the PARENT field then that will cause this error. It's a shame that the error message is not a bit more helpful.

    If you are unsure, then backup the table that has the Foreign Key, delete all the data and then try to create the Foreign Key. If successful then you what to do!

    Good luck.

    0 讨论(0)
  • 2020-11-22 02:23

    I'm guessing that Clients.Case_Number and/or Staff.Emp_ID are not exactly the same data type as Clients_has_Staff.Clients_Case_Number and Clients_has_Staff.Staff_Emp_ID.

    Perhaps the columns in the parent tables are INT UNSIGNED?

    They need to be exactly the same data type in both tables.

    0 讨论(0)
  • 2020-11-22 02:23

    I just wanted to add this case as well for VARCHAR foreign key relation. I spent the last week trying to figure this out in MySQL Workbench 8.0 and was finally able to fix the error.

    Short Answer: The character set and collation of the schema, the table, the column, the referencing table, the referencing column and any other tables that reference to the parent table have to match.

    Long Answer: I had an ENUM datatype in my table. I changed this to VARCHAR and I can get the values from a reference table so that I don't have to alter the parent table to add additional options. This foreign-key relationship seemed straightforward but I got 1215 error. arvind's answer and the following link suggested the use of

    SHOW ENGINE INNODB STATUS;
    

    On using this command I got the following verbose description for the error with no additional helpful information

    Cannot find an index in the referenced table where the referenced columns appear as the first columns, or column types in the table and the referenced table do not match for constraint. Note that the internal storage type of ENUM and SET changed in tables created with >= InnoDB-4.1.12, and such columns in old tables cannot be referenced by such columns in new tables. Please refer to http://dev.mysql.com/doc/refman/8.0/en/innodb-foreign-key-constraints.html for correct foreign key definition.

    After which I used SET FOREIGN_KEY_CHECKS=0; as suggested by Arvind Bharadwaj and the link here:

    This gave the following error message:

    Error Code: 1822. Failed to add the foreign key constraint. Missing index for constraint

    At this point, I 'reverse engineer'-ed the schema and I was able to make the foreign-key relationship in the EER diagram. On 'forward engineer'-ing, I got the following error:

    Error 1452: Cannot add or update a child row: a foreign key constraint fails

    When I 'forward engineer'-ed the EER diagram to a new schema, the SQL script ran without issues. On comparing the generated SQL from the attempts to forward engineer, I found that the difference was the character set and collation. The parent table, child table and the two columns had utf8mb4 character set and utf8mb4_0900_ai_ci collation, however, another column in the parent table was referenced using CHARACTER SET = utf8 , COLLATE = utf8_bin ; to a different child table.

    For the entire schema, I changed the character set and collation for all the tables and all the columns to the following:

    CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci;
    

    This finally solved my problem with 1215 error.

    Side Note: The collation utf8mb4_general_ci works in MySQL Workbench 5.0 or later. Collation utf8mb4_0900_ai_ci works just for MySQL Workbench 8.0 or higher. I believe one of the reasons I had issues with character set and collation is due to MySQL Workbench upgrade to 8.0 in between. Here is a link that talks more about this collation.

    0 讨论(0)
  • 2020-11-22 02:24

    Check the collation of table, using SHOW TABLE STATUS you can check information about the tables, including the collation.

    Both tables have to has the same collation.

    It's happened to me.

    0 讨论(0)
  • 2020-11-22 02:25

    I can not find this error

    CREATE TABLE RATING (
    
    Riv_Id INT(5),
    Mov_Id INT(10) DEFAULT 0,
    Stars INT(5),
    Rating_date DATE, 
    
    PRIMARY KEY (Riv_Id, Mov_Id),
    
    FOREIGN KEY (Riv_Id) REFERENCES REVIEWER(Reviewer_ID)
    ON DELETE SET NULL ON UPDATE CASCADE,
    
    FOREIGN KEY (Mov_Id) REFERENCES MOVIE(Movie_ID)
    ON DELETE SET DEFAULT ON UPDATE CASCADE
    )
    
    0 讨论(0)
  • 2020-11-22 02:25

    So I tried all the fixes above and no luck. I may be missing the error in my tables -just could not find the cause and I kept getting error 1215. So I used this fix.

    In my local environment in phpMyAdmin, I exported data from the table in question. I selected format CSV. While still in phpMyAdmin with the table selected, I selected "More->Options". Here I scrolled down to "Copy table to (database.table). Select "Structure only". Rename the table something, maybe just add the word "copy" next to the current table name. Click "Go" This will create a new table. Export the new table and import it to the new or other server. I am also using phpMyAdmin here also. Once imported change the name of the table back to its original name. Select the new table, select import. For format select CSV. Uncheck "enable foreign key checks". Select "Go". So far all is working good.

    I posted my fix on my blog.

    0 讨论(0)
提交回复
热议问题