MySQL Error 1215: Cannot add foreign key constraint

前端 未结 30 2845
予麋鹿
予麋鹿 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条回答
  •  闹比i
    闹比i (楼主)
    2020-11-22 02:20

    Reasons you may get a foreign key constraint error:

    1. You are not using InnoDB as the engine on all tables.
    2. You are trying to reference a nonexistent key on the target table. Make sure it is a key on the other table (it can be a primary or unique key, or just a key)
    3. The types of the columns are not the same (exception is the column on the referencing table can be nullable even if it is not nullable on the referenced table).
    4. If the PK/FK is a varchar make sure the collation is the same for both.

    Update:

    1. One of the reasons may also be that the column you are using for ON DELETE SET NULL is not defined to be null. So make sure that the column is set default null.

    Check these.

提交回复
热议问题