MySQL throws ER_JSON_USED_AS_KEY when a JSON column is placed in a PRIMARY KEY, UNIQUE KEY, or ordinary INDEX definition.
ER_JSON_USED_AS_KEY appears in MySQL when you put a JSON column in a PRIMARY KEY, UNIQUE KEY, or INDEX. Remove the JSON field from the key or create a generated column that extracts a scalar value and index that instead.
ER_JSON_USED_AS_KEY
MySQL error 3152, condition name ER_JSON_USED_AS_KEY, occurs when a table definition attempts to include a JSON column in a primary key, unique key, or secondary index. The JSON data type was introduced in MySQL 5.7.8, but the optimizer forbids direct indexing of JSON columns.
The restriction exists because JSON is a complex document type with variable length and structure. Indexes require stable, deterministic byte sequences, which JSON values cannot guarantee without explicit extraction of scalar elements.
The error appears during CREATE TABLE, ALTER TABLE, or CREATE INDEX statements that list a JSON column inside the key definition. Execution stops immediately, and the table or index creation fails.
Developers usually see it while migrating schemas, adding uniqueness constraints, or porting examples that work in other databases where JSON indexing is supported natively.
Leaving the schema unchanged blocks table creation or alteration, halting deployment pipelines. Additionally, misunderstanding the limitation may lead to inefficient full-table scans instead of proper indexing, degrading query performance.
Defining a composite primary key that mistakenly includes a JSON column triggers the error.
Attempting to enforce uniqueness directly on a JSON column fails because MySQL cannot compare complex documents deterministically.
CREATE INDEX idx_user_meta ON users (user_meta) produces error 3152 as soon as user_meta is typed JSON.
ORMs that auto-map JSON to text-like types in other databases may emit invalid MySQL DDL when run against a MySQL backend.
Occurs when the JSON document itself is malformed, not when it is mis-indexed.
Raised when extracted numeric values exceed supported ranges.
Appears when an unavailable index type is chosen; sometimes confused with JSON indexing issues.
No. You must use a generated column that extracts a scalar value or compute a hash if you need uniqueness on the entire JSON document.
All versions starting from 5.7.8 up to the latest MySQL 8.x currently enforce the restriction.
As of MySQL 8.4 LTS, Oracle has not announced direct JSON key support. Generated columns remain the recommended approach.
Galaxy flags invalid DDL instantly and its AI copilot proposes correct generated column syntax, preventing ER_JSON_USED_AS_KEY from reaching production.