3206
MariaDBERRORNotableDDLHIGH confidence

Invalid foreign key option for generated column

Production Risk

Low — DDL error.

Why it happens
  1. 1Defining a foreign key constraint on a virtual generated column.
  2. 2FK actions like ON UPDATE SET NULL or ON DELETE CASCADE are incompatible with generated columns.
How to reproduce
trigger — this will error
trigger — this will error
ALTER TABLE t1 ADD CONSTRAINT fk1 FOREIGN KEY (gen_col) REFERENCES t2(id) ON UPDATE SET NULL;

expected output

ERROR 3206 (HY000): Foreign key constraint on a generated column not allowed with this option.

Fix 1

Use a stored generated column if FK is required

Use a stored generated column if FK is required
ALTER TABLE t1 MODIFY gen_col INT AS (expr) STORED;

Why this works

Stored generated columns have fewer FK restrictions than virtual ones.

Fix 2

Remove incompatible ON UPDATE/ON DELETE clauses

Remove incompatible ON UPDATE/ON DELETE clauses
ALTER TABLE t1 ADD CONSTRAINT fk1 FOREIGN KEY (gen_col) REFERENCES t2(id);

Why this works

Simple FK references without cascade actions may work on stored generated columns.

What not to do

Sources
Official documentation ↗

MySQL 8.0 — 3206 ER_WRONG_FK_OPTION_FOR_GENERATED_COLUMN3

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

← All MariaDB errors