1169
MariaDBERRORNotableConstraintHIGH confidence

Can't write because of unique constraint

Production Risk

Medium — write is rejected; application must handle the error.

What this means

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.

Why it happens
  1. 1Inserting or updating a row that would create a duplicate value in a UNIQUE indexed column
  2. 2Bulk load containing duplicate values for a unique-constrained column
How to reproduce
trigger — this will error
trigger — this will error
INSERT INTO t (email) VALUES ('dup@example.com');
INSERT INTO t (email) VALUES ('dup@example.com'); -- duplicate

expected 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.

Use INSERT IGNORE or ON DUPLICATE KEY UPDATE
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.

What not to do

Remove the UNIQUE constraint to avoid 1169

UNIQUE constraints enforce data integrity; removing them allows duplicate data that may cause application-level bugs.

Sources
Official documentation ↗

MySQL 8.0 — 1169 ER_DUP_UNIQUE

Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev

← All MariaDB errors