Skip to main content

MySQL error code 121: Duplicate key on write or update

· One min read
Stephan Hochdörfer

While migrating a Magento module to make use of the declarative db schema, I ran into the following MySQL error: MySQL error code 121: Duplicate key on write or update

What happened? I had installed the module in question locally and the respective database table got created via the InstallSchema script that comes with the module. To make sure that the database table created by the declarative db schema is identical, I decided to rename the database table and keep it as a backup. This way I could compare both tables to make sure that they are indeed identical.

After setting up the declarative db schema xml file and running ./bin/magento setup:upgrade, the upgrade process died with the MySQL error mentioned above.

At first, I had no clue what happened and it took a while to find the correct answer. The table in question had a foreign key constraint defined and apparently, the constraint names need to be unique in the database. You either have to rename the constraint or delete the table with the constraint. I did the latter and ./bin/magento setup:upgrade worked fine again.