How to Fix MySQL Error 3733 (ER_FK_CANNOT_USE_VIRTUAL_COLUMN)

Published
Length
3 min read

Luis Felipe Dias

All posts

You trigger mysql error 3733 when you build a foreign key on a virtual column. To fix it, change the virtual column to a stored column or a normal column. MySQL lets you create generated columns that calculate values from other fields in the row. These generated columns can be either virtual or stored on the disk. When a schema change tries to link a virtual column to another table, the engine stops. The database returns the symbol ER_FK_CANNOT_USE_VIRTUAL_COLUMN under SQLSTATE HY000 and blocks

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 3733 when you build a foreign key on a virtual column. To fix it, change the virtual column to a stored column or a normal column.

MySQL lets you create generated columns that calculate values from other fields in the row. These generated columns can be either virtual or stored on the disk. When a schema change tries to link a virtual column to another table, the engine stops. The database returns the symbol ER_FK_CANNOT_USE_VIRTUAL_COLUMN under SQLSTATE HY000 and blocks the query.

What causes mysql error 3733 in the database engine

The official message states that a foreign key uses a virtual column, which is not supported. MySQL computes virtual column values on the fly when you read rows from a table. The engine does not write these virtual values to disk inside the table space.

Foreign keys need real data on disk to check links between parent and child tables. Cascading updates and deletes also need to change values stored in the table rows. Because virtual columns hold no physical data, the storage engine cannot enforce these rules. Even if you put an index on the virtual column, the foreign key still fails.

Developers often hit this issue when pulling keys out of JSON documents for table joins. A team might create a virtual column from a JSON field and add a constraint. The migration script then fails right away with code 3733 during deployment.

How to diagnose and fix the column failure

First, find the exact constraint name and column name in the error message output. Check your table schema to see how the target column generates its values. Look for columns defined as virtual in either the child table or the parent table.

You have two clean ways to fix this schema problem in your database. The first option is to convert the virtual column into a stored generated column. Stored columns calculate their values when rows are written and save the result on disk. Because the data sits on disk, MySQL allows foreign keys on stored columns.

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

Continue reading? Get the full guide.

Free. No spam. Unsubscribe anytime.

Keep in mind that cascading actions still have limits on stored generated columns. If a foreign key tries to update or set null on a stored column, it fails. This happens because a generated column only changes when its base columns change. Use restrict or no action rules on the foreign key when using stored columns.

The second option is to replace the virtual column with a standard physical column. Have your app write the value directly into that normal column during inserts and updates. A standard column supports all foreign key rules and cascading actions without any engine limits.

Safe schema changes in production

Changing a virtual column to a stored column rebuilds the whole table in MySQL. On a big production table, this rebuild can lock writes and slow down your app. Never turn off foreign key checks to force a broken schema change through.

Instead, test the schema migration in a staging setup with a copy of production data. Check how long the table rebuild takes and how much extra disk space it needs. Run the change during low traffic hours or use an online schema change tool.

Give your migration tool only the schema permissions it needs for that single database. Do not grant full admin rights to app roles just to fix a migration error. Keep strict access controls and review every schema change before it runs in production.

Inspect every MySQL query and response in flight

Static grants and network rules block bad logins. They cannot pause a risky query for review or mask emails in a result set. By design, hoop.dev sits inline in the MySQL protocol as a sidecar for your MySQL. For every plain query, prepared statement, or cursor an agent or human runs in production, hoop.dev decides to pass, mask, block, or hold it for review.

First, hoop.dev guardrails block banned actions before MySQL runs them, and an AI Analyzer you configure can score intent or escalate for approval. Next, hoop.dev data masking rewrites sensitive string columns in results in memory, and every verdict goes to the audit trail. Find the open-source binary on GitHub.

Frequently asked questions

Can I index a virtual column in MySQL without a foreign key?

Yes, MySQL lets you build secondary indexes on virtual columns to speed up read queries. You only hit error 3733 when you try to attach a foreign key constraint.

Does switching to a stored column use more disk space?

Yes, a stored column writes its calculated value to disk for every row in the table. Make sure your database server has enough free storage before you convert a large table.

Can a foreign key reference a virtual column in the parent table?

No, neither the child table nor the parent table can use a virtual column here. Both sides of the foreign key link must use physical or stored columns on disk.

The query runs. The emails stay hidden.