How to Fix MySQL Error 4112 (ER_CREATE_SELECT_WITH_GIPK_DISALLOWED_IN_SBR)
You get mysql error 4112 when a table creation query tries to make an invisible primary key during statement-based logging. You can fix it by adding an explicit primary key, splitting the query, or switching to row-based logging. This error stops table creation scripts in their tracks. It happens when two safety rules in your database clash. One rule forces every new table to have a primary key. The other rule controls how the database writes changes to its log. Why mysql error 4112 happens i
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 get mysql error 4112 when a table creation query tries to make an invisible primary key during statement-based logging. You can fix it by adding an explicit primary key, splitting the query, or switching to row-based logging.
This error stops table creation scripts in their tracks. It happens when two safety rules in your database clash. One rule forces every new table to have a primary key. The other rule controls how the database writes changes to its log.
Why mysql error 4112 happens in your database
The database returns error code 4112 under general state HY000. The official symbol is ER_CREATE_SELECT_WITH_GIPK_DISALLOWED_IN_SBR. The engine blocks invisible primary keys on tables made from select queries when statement-based logging is active.
Modern database setups often turn on generated invisible primary keys. This feature adds a hidden key to any new table that lacks one. Hidden keys help replication run fast and keep data safe. Still, this feature has strict limits when combined with older logging modes.
Your database writes every data change to a binary log. In statement-based logging, the server logs the exact query text you run. In row-based logging, the server logs the actual changed rows instead. Statement-based logs need queries to run the exact same way on every replica.
A query that creates a table and inserts rows from a select query is tricky. The select part does not promise a fixed row order. If the database generates a hidden key during this step, replicas might number rows differently. To stop replica drift, the engine blocks the query and throws mysql error 4112.
How to diagnose the logging conflict
First, check how your session writes to the binary log. An app or user script might force statement-based logging for its own session. Even if the main server uses row-based logging, a session override can trigger this bug.
Next, check if your server forces generated invisible primary keys. Managed database services often turn this rule on to protect backups and replicas. You can read the active session settings to confirm both rules are on at once.
Just-in-Time Access + MySQL Access Governance: architecture patterns and best practices
Continue reading? Get the full guide.
Free. No spam. Unsubscribe anytime.
Look at the failed query in your app logs or deployment scripts. You will see a single query that builds a table and fills it with data. That combined command is the direct trigger for the crash.
How to fix mysql error 4112 safely in production
Do not turn off primary key rules or grant admin roles to app users. Weakening database rules to pass one query creates big risks later. Instead, fix the query pattern or adjust the logging format safely.
The best fix is to split the single query into two separate steps. First, run a plain command to create the empty table with an explicit primary key. Second, run an insert query to load the rows from your select query. This two-step method works in all logging modes and keeps replicas in sync.
You can also define an explicit primary key right inside the combined creation query. When you name your own primary key, the engine does not make a hidden one. Because no invisible key is made, the database lets the query finish.
If you cannot change the app code, switch the binary log format to row-based mode. Row-based logging records the exact row values and key numbers sent to each replica. This removes the risk of out-of-order keys and stops the error right away. Have a database admin update the server rule so app accounts stay restricted.
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
What does ER_CREATE_SELECT_WITH_GIPK_DISALLOWED_IN_SBR mean?
This symbol means your query tried to build a table from a select result while using hidden keys and statement logs. The database blocks this mix so replica servers do not get mismatched key numbers.
Should I disable generated invisible primary keys to fix the error?
No, turning off invisible primary keys can hurt replica speed and break cloud backup tools. Instead, add a visible primary key to your new table or split the query into two steps.
Why did this query work in my test setup but fail in production?
Test servers often have binary logging turned off or use different key generation rules. Production clusters enforce strict logging and key rules to keep high availability replicas safe.
Related guides
In this post