Sunday, November 7, 2010

MySQL ERROR 1061: Duplicate key name 'my_tbl_fk'

I was creating a foreign key in MySQL Workbench 5.2 OSS when I got the following error:

ERROR 1061: Duplicate key name 'tbl1_tbl2_fk'

I checked the "Foreign Keys" tab for the table to which I was adding the FK and there was no key with that name. In fact, there were no foreign keys with that name anywhere.

However, when I checked the indexes on that table I found the problem: an index with the same name as the foreign key I was creating. I dropped that index and was able to create the foreign key without any issues.

What happened was I had previously created a foreign key with that name and then dropped it on the 'Foreign Keys' tab. The index backing this key was not dropped when I did that, so when I attempted to recreate the foreign key with that name it tried to duplicate the index.

No comments:

Post a Comment