3349
MySQLERRORNotableDDL / IndexesHIGH confidence

Functional index cannot use a JSON or geometry return-type function

Production Risk

Low — DDL fails; no index is created.

How to reproduce
trigger — this will error
trigger — this will error
CREATE INDEX idx ON t ((JSON_EXTRACT(doc, '$.key')));

expected output

ERROR 3349 (HY000): Functional index on a JSON or geometry expression is not allowed.

Fix

Use a generated column

Use a generated column
ALTER TABLE t ADD COLUMN key_val VARCHAR(100) GENERATED ALWAYS AS (JSON_UNQUOTE(JSON_EXTRACT(doc, '$.key'))) VIRTUAL; CREATE INDEX idx ON t (key_val);

Why this works

A generated column stores the scalar extraction, making it indexable.

What not to do

Sources
Official documentation ↗

MySQL 8.0 — 3349 ER_FUNCTIONAL_INDEX_ON_JSON_OR_GEOMETRY_FUNCTION

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

← All MySQL errors