4114
MySQLERRORNotableHIGH confidence

Cannot add foreign key on virtual generated column

Production Risk

Low — DDL fails; no existing data is modified.

Why it happens
  1. 1A foreign key was defined on a virtual generated column in a CREATE TABLE or ALTER TABLE statement.

Fix

Convert the virtual column to a stored column or use a real column

Convert the virtual column to a stored column or use a real column
ALTER TABLE t MODIFY col INT STORED, ADD FOREIGN KEY (col) REFERENCES other(id);

Why this works

Stored generated columns or regular columns can participate in FK constraints.

What not to do

Sources
Official documentation ↗

MySQL 8.0 — 4114 ER_CANNOT_ADD_FOREIGN_KEY_ON_VIRTUAL_COLUMN2

Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev

← All MySQL errors