Why Generated Columns doesn’t support NOW() function?
I tried:
CURRENT_DATE()
CURRENT_TIMESTAMP()
CURRENT_TIMESTAMP()
CURDATE()
NOW()
LOCALTIMESTAMP
I’ve got error:
Update column failed. Expression of generated column ‘currentWeekDay’ contains a disallowed function.
I want to create simple column:
WEEKDAY(CURDATE())
One of the rules applied to generated columns is that the expression must use deterministic functions.
What is a deterministic function? Here’s a definition straight from the underlying database docs:
A function is deterministic if, given the same data in tables, multiple invocations produce the same result, independently of the connected user. Examples of functions that are nondeterministic and fail this definition: NOW()
, CURRENT_DATE()
It is still possible to create a generated column as you describe it, however, instead of CURDATE()
use the created
or the update
columns, for example:
WEEKDAY( created )
Regards,
Mark
In MariaDB, this works in the following way
There are two type of generated column: STORED and VIRTUAL.
You can do it using VIRTUAL type.
ALTER TABLE MY_TABLE ADD COLUMN MY_VIRTUAL_COLUMN_NAME VARCHAR(50) GENERATED ALWAYS AS (
CURRENT_DATE
) VIRTUAL;