MySQL Error 1215: Cannot add foreign key constraint

前端 未结 30 2721
予麋鹿
予麋鹿 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:11

    Another source of this error is that you have 2 or more same table names which have the same foreign key names. This sometimes happens to people who use modelling and design software, like Mysql Workbench, and later generate the script from the design.

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

    There is a pitfall I have experienced with "Error 1215: Cannot add foreign key constraint" when using Laravel 4, especially with JeffreyWay's Laravel 4 Generators.

    In Laravel 4, you can use JeffreyWay's Generators to generate migration files to create tables one-by-one, which means, each migration file generates one table. You have to be aware of the fact that each migration file is generated with a timestamp in the filename, which gives the files an order. The order of generation is also the order of migration operation when you fire the Artisan CLI command "php artisan migrate". So, if a file asks for a foreign key constraint referring to a key which will be, but not yet, generated in a latter file, the Error 1215 is fired. In such case, what you have to do is adjust the order of migration files generation. Generate new files in proper order, copy-in the content, then delete the disordered old files.

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

    Be aware of the use of backquotes too. I had in a script the following statement

    ALTER TABLE service ADD FOREIGN KEY (create_by) REFERENCES `system_user(id)`;
    

    but the backquotes at the end were false. It should have been:

    ALTER TABLE service ADD FOREIGN KEY (create_by) REFERENCES `system_user`(`id`);
    

    MySQL give unfortunalty no details on this error...

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

    For others same error may not always be due to column type mismatch, you can find out more information about a mysql foriegn key error by issuing command

    SHOW ENGINE INNODB STATUS;
    

    you may find a error near the top of the printed message something like

    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.

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

    Error 1215 is an annoying one. Explosion Pill's answer covers the basics. You want to make sure to start from there. However, there are more, much more subtle cases to look out for:

    For example, when you try to link up PRIMARY KEYs of different tables, make sure to provide proper ON UPDATE and ON DELETE options. E.g.:

    ...
    PRIMARY KEY (`id`),
    FOREIGN KEY (`id`) REFERENCES `t` (`other_id`) ON DELETE SET NULL
    ....
    

    won't fly, because PRIMARY KEYs (such as id) can't be NULL.

    I am sure, there are even more, similarly subtle issues when adding these sort of constraints, which is why when coming across constraint errors, always make sure that the constraints and their implications make sense in your current context. Good luck with your error 1215!

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

    i had the same issue, my solution:

    Before:

    CREATE TABLE EMPRES
    ( NoFilm smallint NOT NULL
    
      PRIMARY KEY (NoFilm)
    
      FOREIGN KEY (NoFilm) REFERENCES cassettes
    
    );
    

    Solution:

    CREATE TABLE EMPRES
    (NoFilm smallint NOT NULL REFERENCES cassettes,
    
     PRIMARY KEY (NoFilm)
    
    );
    

    I hope it's help ;)

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