Common SQL Errors

MySQL Error 3102: ER_GENERATED_COLUMN_FUNCTION_IS_NOT_ALLOWED - How to Fix

Galaxy Team
August 8, 2025

A generated column expression uses a disallowed or nondeterministic function, so MySQL rejects the CREATE or ALTER TABLE statement.

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 3102 ER_GENERATED_COLUMN_FUNCTION_IS_NOT_ALLOWED?

ER_GENERATED_COLUMN_FUNCTION_IS_NOT_ALLOWED appears when a generated column references a nondeterministic or blocked function like RAND() or NOW(). Replace the forbidden function with a deterministic alternative and rerun the CREATE or ALTER TABLE statement to fix the error.

Error Highlights

Typical Error Message

ER_GENERATED_COLUMN_FUNCTION_IS_NOT_ALLOWED

Error Type

Invalid Function Error

Language

MySQL

Symbol

function. ER_GENERATED_COLUMN_FUNCTION_IS_NOT_ALLOWED was added in 5.7.6.

Error Code

3102

SQL State

HY000

Explanation

Table of Contents

What is MySQL error 3102 ER_GENERATED_COLUMN_FUNCTION_IS_NOT_ALLOWED?

MySQL raises error 3102 when a generated column expression contains a function that is nondeterministic, depends on connection context, or is explicitly disallowed. Examples include RAND(), NOW(), UUID(), or subqueries. The server rejects the CREATE or ALTER TABLE statement to protect data consistency and replication safety.

The restriction was introduced in MySQL 5.7.6 alongside generated columns. By blocking unsafe functions, MySQL ensures that every replica computes the same value for a generated column, preventing drift in statement based replication and guaranteeing repeatable reads.

What Causes This Error?

The error occurs whenever a generated column references a function that MySQL marks as non-deterministic or non-pure. Functions that read the current time, random numbers, user variables, or system status produce different results per call, violating determinism. MySQL therefore flags the definition as unsafe.

The error also appears if the expression uses functions needing the SUPER privilege when invoked in stored programs, because generated columns run with the definer's privileges and must stay safe for every user.

How to Fix ER_GENERATED_COLUMN_FUNCTION_IS_NOT_ALLOWED

Identify the forbidden function in the generated column expression. Replace it with a deterministic equivalent or compute the value in application code. For current timestamps, store the value in a regular column with DEFAULT CURRENT_TIMESTAMP and reference that column in the generated expression instead.

After modifying the expression, rerun the CREATE TABLE or ALTER TABLE statement. The table will now be created or altered without raising error 3102.

Common Scenarios and Solutions

Using RAND() to create a random hash fails. Generate the random value in the application and write it to a base column, then let the generated column reference that stored value.

Including NOW() or CURRENT_TIMESTAMP in a date math expression fails. Store the insert timestamp in a normal column with a default, then derive the generated column from that stored timestamp.

Best Practices to Avoid This Error

Keep generated column expressions deterministic and based only on columns of the same row. Avoid calling functions that read external state. Prefer pure string, arithmetic, and JSON functions that MySQL marks as deterministic.

Create unit tests or migration checks that attempt to create tables in a staging database so the error is caught during CI rather than in production.

Related Errors and Solutions

Error 3105 ER_GENERATED_COLUMN_REF_AUTO_INC occurs when a generated column references an AUTO_INCREMENT column in an invalid context. Ensure the reference is allowed or move logic to the application. Error 1293 HY000 sets @@sql_mode restrictions that also affect determinism checks.

Common Causes

Nondeterministic functions

Functions like RAND(), UUID(), and NOW() return different results each call, so MySQL blocks them in generated columns.

Context dependent functions

Functions that rely on user, connection, or server state such as CURRENT_USER() are disallowed.

Subqueries or aggregation

Generated columns cannot execute subqueries or GROUP BY operations because they may change per row or execution.

Related Errors

Error 3105 ER_GENERATED_COLUMN_REF_AUTO_INC

Raised when a generated column references an AUTO_INCREMENT column in a disallowed way. Remove the reference or convert to STORED.

Error 3106 ER_UNSUPPORTED_ACTION_ON_GENERATED_COLUMN

Appears when trying to modify a generated column with UPDATE. Generated columns are read-only and must be altered via DDL.

Error 1293 HY000 SET_OPTION_SQL_MODE

Certain sql_mode settings influence determinism checks and can surface during generated column creation.

FAQs

Can I use NOW() inside a generated column?

No. NOW() is nondeterministic. Store the timestamp in a normal column with DEFAULT CURRENT_TIMESTAMP and reference that column instead.

Which functions are safe in generated columns?

Deterministic functions that depend only on their arguments, such as CONCAT, JSON_EXTRACT, ABS, or arithmetic operators, are allowed.

Does the error affect STORED and VIRTUAL columns equally?

Yes. Both STORED and VIRTUAL generated columns must use deterministic functions.

How does Galaxy help prevent this error?

Galaxy's SQL editor highlights nondeterministic functions in generated column expressions and its AI copilot suggests compliant alternatives before you run the query, avoiding runtime failures.

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