Can't write because of unique constraint
Production Risk
Medium — write is rejected; application must handle the error.
ER_DUP_UNIQUE (1169, SQLSTATE 23000) is raised when an INSERT or UPDATE violates a UNIQUE constraint. It is closely related to 1062 (ER_DUP_ENTRY) but may appear in different storage engine contexts.
- 1Inserting or updating a row that would create a duplicate value in a UNIQUE indexed column
- 2Bulk load containing duplicate values for a unique-constrained column
INSERT INTO t (email) VALUES ('dup@example.com');
INSERT INTO t (email) VALUES ('dup@example.com'); -- duplicateexpected output
ERROR 1169 (23000): Can't write, because of unique constraint, to table 't'
Fix
Use INSERT IGNORE or ON DUPLICATE KEY UPDATE
WHEN Duplicates are expected and should be handled gracefully.
INSERT INTO t (email) VALUES ('dup@example.com')
ON DUPLICATE KEY UPDATE updated_at = NOW();Why this works
ON DUPLICATE KEY UPDATE merges the new values into the existing row when a unique conflict occurs.
✕ Remove the UNIQUE constraint to avoid 1169
UNIQUE constraints enforce data integrity; removing them allows duplicate data that may cause application-level bugs.
MySQL 8.0 — 1169 ER_DUP_UNIQUE
Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev