How to Fix MySQL Error 1022 (ER_DUP_KEY)

Published
Length
3 min read

Luis Felipe Dias

All posts

A mysql error 1022 happens when a write operation hits a duplicate key constraint. You fix it by renaming conflicting constraint names or resolving duplicate row values. The database returns the symbol ER_DUP_KEY and uses the SQLSTATE code 23000. The full message states that the engine cannot write due to a duplicate key. While this sounds like a simple row conflict, schema changes often trigger it too. Understanding both causes helps your team restore normal database writes without risking dat

Your coding agents run with your access.

Claude Code reaches prod with a developer's credentials, and approval prompts get clicked through. hoop.dev checks every query, masks sensitive data, and holds risky changes for a human.

A mysql error 1022 happens when a write operation hits a duplicate key constraint. You fix it by renaming conflicting constraint names or resolving duplicate row values.

The database returns the symbol ER_DUP_KEY and uses the SQLSTATE code 23000. The full message states that the engine cannot write due to a duplicate key. While this sounds like a simple row conflict, schema changes often trigger it too. Understanding both causes helps your team restore normal database writes without risking data loss.

What causes mysql error 1022 in your database

In MySQL, foreign key constraint names must be unique across the entire database schema. Many engineers assume constraint names only apply to a single table. When you create a new table with an existing constraint name, the write fails. The database engine blocks the schema change and throws the ER_DUP_KEY failure message.

Data writes can also trigger this error during normal app operations. An insert or update might try to write a key that already exists. Temporary tables created during complex read queries can also hit key limits. When an internal temporary table overflows memory, the engine writes it to disk. If that disk write hits a key conflict, the query stops right away.

How to find the duplicate key conflict safely

Start by checking the exact query that triggered the failure in your log. If the failed command is a schema change, inspect your existing constraint names. Query the information schema views to list all active constraints in that database. Look for another table that already uses the same foreign key name.

If a standard data write caused the error, check the target table indexes. Identify which unique index or primary key rejected the new data. You do not need to grant full admin rights to check table structures. A read role on the metadata catalog lets engineers find the conflict safely. If the table holds sensitive fields like emails, phone numbers, or card numbers, apply data masking. Masking rewrites matched fields in results so engineers can debug without exposing raw user data.

API Key Management + MySQL Access Governance: architecture patterns and best practices

Continue reading? Get the full guide.

Free. No spam. Unsubscribe anytime.

How to fix the error in production

To fix a schema change conflict, pick a unique name for the foreign key. A good rule is to include both table names in the constraint label. This habit prevents future naming collisions when your team adds more tables later. Once you update the constraint name in your migration script, run the change again.

For data write conflicts, review how your app generates and sends record keys. Make sure your app logic handles existing records with an update path when needed. Never disable foreign key checks or unique checks in production just to force writes. Turning off safety checks leaves broken links and corrupt records in your tables. Instead, test your fix in a staging setup and run the clean query through a reviewed workflow.

Inspect every MySQL query and response in flight

Static grants and network rules block bad logins. They cannot pause a risky query for review or mask emails in a result set. By design, hoop.dev sits inline in the MySQL protocol as a sidecar for your MySQL. For every plain query, prepared statement, or cursor an agent or human runs in production, hoop.dev decides to pass, mask, block, or hold it for review.

First, hoop.dev guardrails block banned actions before MySQL runs them, and an AI Analyzer you configure can score intent or escalate for approval. Next, hoop.dev data masking rewrites sensitive string columns in results in memory, and every verdict goes to the audit trail. Find the open-source binary on GitHub.

Frequently asked questions

How is mysql error 1022 different from error 1062?

Error 1022 often points to a duplicate foreign key constraint name during schema changes. Error 1062 almost always means a data row violated a unique index during a write.

Can a read query trigger ER_DUP_KEY in MySQL?

Yes, a complex read query can create an internal temporary table on disk. If that temporary table hits a key conflict while writing rows, the query fails with code 1022.

Should I turn off foreign key checks to bypass error 1022?

No, disabling foreign key checks does not fix duplicate constraint names in a schema. It also risks data integrity by allowing broken links between your production tables.

The query runs. The emails stay hidden.