Common SQL Errors

MySQL Error 3103 ER_KEY_BASED_ON_GENERATED_COLUMN - How to Fix and Prevent

Galaxy Team
August 8, 2025

The server rejects creating an index on a virtual generated column in versions where this restriction exists.

Sign up for the latest in common SQL errors from the Galaxy Team!
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.

What is MySQL error 3103 ER_KEY_BASED_ON_GENERATED_COLUMN?

MySQL error 3103 ER_KEY_BASED_ON_GENERATED_COLUMN occurs when you try to add a key or index to a virtual generated column in 5.7.6 and select 5.7.7 builds. Convert the column to STORED or do not index it to resolve the problem.

Error Highlights

Typical Error Message

ER_KEY_BASED_ON_GENERATED_COLUMN

Error Type

DDL Error

Language

MySQL

Symbol

column. ER_KEY_BASED_ON_GENERATED_COLUMN was added in 5.7.6, removed after 5.7.7.

Error Code

3103

SQL State

HY000

Explanation

Table of Contents

What is MySQL error 3103 ER_KEY_BASED_ON_GENERATED_COLUMN?

MySQL throws error 3103 when a CREATE TABLE or ALTER TABLE statement tries to place a PRIMARY KEY, UNIQUE KEY, or secondary index on a virtual generated column. The restriction was introduced in 5.7.6 and lifted shortly after 5.7.7, so only builds within that window are affected today.

The message signals that the storage engine cannot maintain an index for a column whose value is computed at read time rather than persisted on disk. Understanding the version-specific rule is critical before changing table design or upgrading a database.

What Causes This Error?

The most common trigger is adding INDEX or KEY definitions on a column declared as GENERATED ALWAYS AS (...) VIRTUAL. Because virtual columns are derived on the fly, MySQL 5.7.6 refused to permit any key maintenance on them.

Another source is importing a dump created on a newer MySQL release into a 5.7.6 instance. The schema may include allowed indexes in later versions that are illegal in 5.7.6.

How to Fix MySQL Error 3103

Convert the generated column to STORED so MySQL writes the computed value to disk, then re-add the desired key. STORED columns behave like normal columns for indexing purposes.

If the column must stay virtual, drop the attempted index and rely on indexing the underlying base columns instead.

Common Scenarios and Solutions

During green-field schema design, declare generated columns as STORED whenever they need to participate in UNIQUE constraints.

When restoring a schema, upgrade the destination server to 5.7.8 or newer where indexing virtual columns is permitted.

Best Practices to Avoid This Error

Track server version capabilities before writing DDL. MySQL 5.7.8+ and all 8.0 versions allow indexes on virtual columns.

Use CI migrations in Galaxy to test DDL against the exact production version, catching errors earlier in development.

Related Errors and Solutions

Error 3106 ER_WRONG_FK_COL_TYPE appears when foreign keys reference mismatched column types. Align the data types to fix.

Error 3102 ER_UNSUPPORTED_ALTER_INPLACE is thrown if an online ALTER TABLE option is impossible. Remove ALGORITHM=INPLACE or use copy-based ALTER.

Common Causes

Virtual generated column indexed

A CREATE TABLE or ALTER TABLE statement explicitly declares KEY(col_virtual).

Schema dump from newer MySQL imported to 5.7.6

Dump contains keys on virtual columns that are only valid in later releases.

Mistaken assumption about virtual column capabilities

Developer assumes virtual and stored generated columns share identical indexing rules in all versions.

Related Errors

ER_GENERATED_COLUMN_NON_PRIOR

Raised when a generated column references an undefined column. Define referenced columns first.

ER_UNSUPPORTED_ACTION_ON_GENERATED_COLUMN

Appears when attempting unsupported operations like DROP on required generated columns. Convert to regular columns before alteration.

ER_UNSUPPORTED_ALTER_INPLACE

Signals an online DDL action MySQL cannot execute in place. Remove ALGORITHM=INPLACE or use COPY.

FAQs

Can I ever index a virtual generated column?

Yes, MySQL 5.7.8 and all 8.0 releases support keys on virtual generated columns. Only 5.7.6 through 5.7.7 reject them.

Is a STORED generated column slower?

STORED columns may increase write cost slightly because MySQL persists the computed value, but read queries benefit from normal index lookups.

Does Galaxys editor highlight this problem?

Galaxy surfaces server error 3103 immediately in its results pane and offers AI suggestions to convert the column to STORED or drop the key.

Will upgrading break existing virtual column indexes?

No. Moving to 5.7.8+ keeps previously created indexes intact and lets you add new ones.

Start Querying with the Modern SQL Editor Today!
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.

Check out some other errors

Trusted by top engineers on high-velocity teams
Aryeo Logo
Assort Health
Curri
Rubie Logo
Bauhealth Logo
Truvideo Logo