How to Fix MySQL Error 1822 (ER_FK_NO_INDEX_PARENT)

Published
Length
3 min read

Luis Felipe Dias

All posts

You get mysql error 1822 when you point a foreign key at an unindexed parent column. You fix it by adding a unique key or index to that parent column first. Database changes often fail during deployments when parent tables lack the right indexes. Knowing why the engine blocks this change helps your team fix schema migrations fast. What causes mysql error 1822 in the database engine MySQL logs this failure under the symbol ER_FK_NO_INDEX_PARENT and SQLSTATE HY000. The engine blocks the foreig

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 1822 when you point a foreign key at an unindexed parent column. You fix it by adding a unique key or index to that parent column first.

Database changes often fail during deployments when parent tables lack the right indexes. Knowing why the engine blocks this change helps your team fix schema migrations fast.

What causes mysql error 1822 in the database engine

MySQL logs this failure under the symbol ER_FK_NO_INDEX_PARENT and SQLSTATE HY000. The engine blocks the foreign key constraint because the referenced parent table lacks a matching index.

Relational engines need an index on the parent column to check data fast. When you insert or update a child row, the engine checks the parent table. Without an index, the database must scan the whole parent table on every write. That full table scan hurts write speed and locks rows for too long.

This problem often shows up when a foreign key points to a non-primary column. Primary keys get an index by default, so they do not trigger this error. If you link to another column like an email or code, you must index it.

Composite foreign keys also trigger this error if the column order does not match. The parent index must list the columns in the exact same starting order. Even a small mismatch in the column list will make the schema change fail.

How to check the parent schema safely

Before changing any table, inspect the parent and child table definitions in your database. Check the parent table to see which columns have a primary or unique key. Look at the child table to confirm which columns you want to link.

Do not turn off foreign key checks to push a broken migration through. Skipping checks leaves your database in a bad state and hides real design flaws. Always fix the root cause on the parent table before adding the child constraint.

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

Continue reading? Get the full guide.

Free. No spam. Unsubscribe anytime.

Check the data types on both tables while you review the schema. Both columns must share the exact same type, length, and sign rules. Fixing both the index and the data types at once prevents follow-up errors.

How to fix the missing index in production

The clean fix is to create a unique key on the referenced parent column. A unique key makes sure each parent value points to just one record. Standard indexes also work in some engines, but unique keys are much safer.

If the parent table already has duplicate values, creating a unique index will fail. You must clean up those duplicate rows before you can add the new key. Run a read query first to find any duplicate values in the parent column.

On large production tables, adding an index can lock writes or slow down queries. Run the index creation during low traffic hours to keep your app fast. Once the parent index is ready, run your migration again to add the foreign key.

Teams should also limit who can run schema changes on live production databases. Granting broad admin rights to fix a broken migration creates big security risks. Use role controls and peer reviews for every database change in your workflow.

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

Can I fix mysql error 1822 by disabling foreign key checks?

No, turning off foreign key checks will not fix a missing parent index. MySQL still requires the parent index to build the constraint structure on the table.

Does the parent column need a primary key or just an index?

The parent column does not need to be the primary key of the table. Still, it must have an index, and a unique key is the best choice.

Why does my composite foreign key still trigger error 1822?

A composite foreign key needs a single parent index covering all referenced columns in order. Separate indexes on each column or a different column order will cause error 1822.

The query runs. The emails stay hidden.