ERROR 1062 (23000): Duplicate entry '[email protected]' for key 'users.email_unique' means a write tried to put a value into a uniquely constrained column (or combination) that already exists in another row. The message names the value and the key, which together identify the rule that fired, a primary key or any UNIQUE index. Nothing is broken: the database is enforcing a rule someone defined, and the application attempted a write that violates it. The work is deciding what a duplicate should mean in this part of the application, then handling it deliberately.
INSERT ... ON DUPLICATE KEY UPDATE when the intent is “insert or refresh”: an upsert that updates the existing row's mutable fields. Right for imports and synchronization, wrong for registrations (it would silently overwrite someone's record).INSERT IGNORE converts the error to a warning and skips the row; appropriate for idempotent bulk loads, dangerous anywhere the application needs to know the write did not happen, and it downgrades other error classes to warnings as well.Why does a retried request trigger it when the first one “failed”? The first attempt often succeeded at the database and failed afterward (a timeout in the response path), so the retry meets the row the first attempt created. Idempotency handling, upserts or checking first, is the systematic fix.
Can duplicates appear on a replica or after restore? Yes, when writes bypass the constraint's protection (broken replication, restored data loaded with checks relaxed, or auto-increment drift). The error is then a symptom; compare the data and counters rather than handling it as an application event.
Which key is 'PRIMARY' in the message? The table's primary key. A 1062 on PRIMARY with auto-generated ids usually means explicit ids were inserted (imports, fixtures) that collide with generated ones, or the auto-increment counter fell behind the data.