How to Fix MySQL Error 1724 (ER_BINLOG_UNSAFE_INSERT_TWO_KEYS)
You trigger mysql error 1724 when an upsert statement runs on a table with multiple unique keys. You can fix this issue by switching your binary log format to row mode or rewriting the query. Database teams often see this warning or failure in replication setups. When the database logs statements instead of rows, two unique keys create confusion. The primary server and the replica might update different rows during the same query. Why mysql error 1724 happens in replication setups MySQL uses
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.
You trigger mysql error 1724 when an upsert statement runs on a table with multiple unique keys. You can fix this issue by switching your binary log format to row mode or rewriting the query.
Database teams often see this warning or failure in replication setups. When the database logs statements instead of rows, two unique keys create confusion. The primary server and the replica might update different rows during the same query.
Why mysql error 1724 happens in replication setups
MySQL uses the symbol ER_BINLOG_UNSAFE_INSERT_TWO_KEYS and SQLSTATE HY000 for this specific failure. The engine flags the query because statement-based logging cannot predict which unique key matches first. If an incoming row conflicts with two distinct unique keys, the storage engine picks one path.
The primary database checks keys in its internal index order. A replica server might hold those indexes in a slightly different physical order. When the replica replays the raw SQL statement, it might match the second unique key instead.
This mismatch causes silent data drift between your primary database and your read replicas. Over time, your apps read bad data from the replica nodes. To protect your data, the engine raises code 1724 and blocks or logs the unsafe query.
How to diagnose the affected tables and queries
Start by checking your database error logs and slow query logs for symbol ER_BINLOG_UNSAFE_INSERT_TWO_KEYS. Look for tables that have a primary key plus at least one extra unique index. Every table with two or more unique constraints can trigger this error during an upsert.
Next, check how your database writes to the binary log. Statement-based logging writes the exact SQL text to the log file. Because the SQL text does not specify which row matched, the replica has to guess.
MySQL Access Governance + End-to-End Encryption: architecture patterns and best practices
Continue reading? Get the full guide.
Free. No spam. Unsubscribe anytime.
You should also review how your app code builds upsert queries. Many object-relational mappers add duplicate key clauses to every write query by default. Finding the exact app service that sends these queries helps you plan a clean fix.
How to fix the error safely in production
You have two safe ways to fix mysql error 1724 without dropping your safety checks. The best engine-level fix is changing the binary log format from statement mode to row mode. Row-based logging records the exact row changes instead of the raw SQL text.
When the primary server logs the exact row change, the replica updates the exact same record. You do not need to grant extra user privileges or turn off replication warnings. Your database admins can apply this change in the server settings without downtime.
If you cannot change the logging mode, you must change how your app writes data. Split the single upsert query into a read query followed by an explicit update or insert. Wrap both steps inside a single transaction so other sessions do not change the row midway.
You can also check if the table truly needs multiple unique keys. Sometimes teams add redundant unique indexes by mistake during schema updates. Removing an unused unique constraint solves the problem right away and speeds up write performance.
Check MySQL queries and results on the wire
MySQL grants control who logs in. They do not check single queries or mask sensitive columns once a session starts. With hoop.dev, you run an open-source proxy, a sidecar for your MySQL, inline in the MySQL wire protocol. On every query an AI agent or human runs against production MySQL, RDS, or Aurora, hoop.dev decides to pass, mask, block, or hold it for review.
Multi-statement batches and prepared queries are split and checked one by one by hoop.dev guardrails, which refuse banned queries with a native MySQL error or hold risky ones for approval. On the response path, hoop.dev data masking rewrites string values in returned rows in memory, and every decision is logged. See the MIT-licensed project on GitHub.
Frequently asked questions
Does a primary key count as a unique key for this error?
Yes, MySQL treats a primary key as a unique key in its storage engine. A table with one primary key and one secondary unique index has two unique keys total.
Can mixed binary logging prevent this replication issue?
Yes, mixed logging switches from statement mode to row mode when it sees an unsafe query. Still, using full row-based logging is safer and more predictable for modern production databases.
Will this error stop my query from running?
In default setups, the engine runs the query and writes a warning to the log. If your session uses strict SQL modes, the database blocks the query and returns an error.
Related guides
In this post