How to Fix MySQL Error 3522 (ER_PK_INDEX_CANT_BE_INVISIBLE)

Published
Length
3 min read

Luis Felipe Dias

All posts

You trigger mysql error 3522 when you try to make a primary key index invisible. You fix this error by keeping the primary key visible and hiding secondary indexes instead. Database teams use invisible indexes to test how queries run before dropping an index. Still, the database engine blocks this setting on primary keys. When a schema change tries to hide a primary key, the engine stops the change. It returns symbol ER_PK_INDEX_CANT_BE_INVISIBLE with SQLSTATE HY000. Why mysql error 3522 happ

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 3522 when you try to make a primary key index invisible. You fix this error by keeping the primary key visible and hiding secondary indexes instead.

Database teams use invisible indexes to test how queries run before dropping an index. Still, the database engine blocks this setting on primary keys. When a schema change tries to hide a primary key, the engine stops the change. It returns symbol ER_PK_INDEX_CANT_BE_INVISIBLE with SQLSTATE HY000.

Why mysql error 3522 happens in the database engine

The official message for this failure states that a primary key index cannot be invisible. In this database engine, the primary key is more than a simple lookup list. The storage engine uses the primary key to build the clustered index for the table. This clustered index holds the actual row data on disk.

The query planner needs this main index to find rows and keep data unique. If you hide the primary key, the engine loses its main path to the table rows. Because of this rule, the engine rejects any schema change that hides a primary key.

You can trigger this error in two ways. The most direct cause is a command that creates or alters a primary key as invisible. A less obvious cause happens when you do not define an explicit primary key. If a table has no primary key, the engine looks for a unique non-null index. The engine promotes the first matching unique index to act as the hidden primary key. If you try to make that specific unique index invisible, the engine blocks you. It returns error code 3522 even though you never named it a primary key.

How to diagnose and fix the failure in production

To fix this error, first inspect the table structure in your schema metadata. Check which column acts as the explicit or promoted primary key for the table. Look at the migration script that failed during your deploy run. Remove the invisible visibility setting from the primary key definition in that script.

If the error happened on a unique non-null index, check if the table lacks a primary key. You can add an explicit visible primary key to the table first. Once the table has a true primary key, the unique index becomes a normal secondary index. You can then make that secondary index invisible to test your query plan.

MySQL Access Governance + End-to-End Encryption: architecture patterns and best practices

Continue reading? Get the full guide.

Free. No spam. Unsubscribe anytime.

Always run schema changes through a controlled review workflow in production. Never grant broad schema alter permissions to everyday developer accounts to bypass deploy errors. Limit table alter rights to your automated deploy tool or approved maintenance sessions. If an engineer inspects table data during diagnosis, make sure your query workflow masks sensitive fields. Data masking rewrites matched fields like emails, phone numbers, card numbers, and national IDs in results.

How to prevent index visibility errors in schema changes

You can stop this error before it reaches production by checking migration scripts in staging. Run your schema changes against a test database that matches your production table structure. Make sure your test tables include the same primary and unique keys as production.

Train your team to check for missing primary keys on older tables before tuning indexes. Adding clear primary keys to every table prevents surprise index promotion rules. When every table has a clear primary key, engineers know which secondary indexes they can hide.

Add wire guardrails and masking beside MySQL

Database grants and TLS setup decide who can reach MySQL. Even so, an open session can run broad updates or read raw personal data from allowed tables. In practice, hoop.dev sits inline in the wire protocol beside the database as a sidecar for your MySQL. On every production query an agent or engineer sends, hoop.dev decides to pass, mask, block, or escalate it for human review.

With hoop.dev MySQL support, guardrails refuse bad queries before they run or hold risky calls for approval, while data masking rewrites matched sensitive values in result rows in memory. Furthermore, hoop.dev holds SOC 2 Type II and generates evidence for GDPR, HIPAA, and PCI DSS programs. Read the docs and source on GitHub.

Frequently asked questions

What does symbol ER_PK_INDEX_CANT_BE_INVISIBLE mean?

This symbol means a query tried to set a primary key index to invisible. The database engine blocks this action and returns error code 3522 with SQLSTATE HY000.

Why did a unique index trigger mysql error 3522?

When a table has no primary key, the engine promotes the first unique non-null index. The engine treats that unique index as the primary key and keeps it visible.

How can I test dropping a primary key if I cannot hide it?

You cannot test dropping a primary key by hiding it in place. Instead, test your new key layout on a copy of the table in a staging database.

The query runs. The emails stay hidden.