1598
MySQLERRORNotableData IntegrityHIGH confidence

Foreign key duplicate entry

Production Risk

Medium — the INSERT fails; referential integrity is preserved.

What this means

A foreign key constraint violation occurred with a duplicate key situation, typically in NDB Cluster replication.

Why it happens
  1. 1Replication of a row that would violate both a UNIQUE constraint and a foreign key.
  2. 2NDB Cluster-specific scenario where a foreign key and duplicate key conflict simultaneously.
How to reproduce
trigger — this will error
trigger — this will error
INSERT INTO child_table (id, parent_id) VALUES (1, 100);
-- when id=1 already exists and parent_id=100 violates FK

expected output

ERROR 1598 (23000): Foreign key constraint for table '%s', record '%s' would lead to a duplicate entry in table '%s'

Fix

Check and resolve the duplicate before inserting

Check and resolve the duplicate before inserting
SELECT * FROM child_table WHERE id = 1;
-- Resolve the duplicate or use ON DUPLICATE KEY UPDATE

Why this works

Ensure the row being inserted does not already exist and that all FK references are valid.

Version notes

Sources
Official documentation ↗

MySQL 8.0 — 1598 ER_FOREIGN_DUPLICATE_KEY

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

← All MySQL errors