MySQL Cannot Add Foreign Key Constraint

后端 未结 22 2104
时光取名叫无心
时光取名叫无心 2020-11-22 08:35

So I\'m trying to add Foreign Key constraints to my database as a project requirement and it worked the first time or two on different tables, but I have two tables on which

22条回答
  •  自闭症患者
    2020-11-22 09:15

    I got the same error. The cause in my case was:

    1. I created a backup of a database via phpmyadmin by copying the whole database.
    2. I created a new db with the same name the old db had und selected it.
    3. I started an SQL script to create updated tables and data.
    4. I got the error. Also when I disabled foreign_key_checks. Altough the database was completely empty.

    The cause was: Since i used phpmyadmin to create some foreign keys in the renamed database - the foreign keys where created with a database name prefix but the database name prefix was not updated. So there were still references in the backup-db pointing to the newly created db.

提交回复
热议问题