mysqlgenerated

Changing generated column to not-generated in MySQL table


I've created a MySQL table that contains a generated column:

CREATE TABLE `messages` (
    `metadata` JSON NOT NULL,
    `aggregate_version` INT(11) UNSIGNED GENERATED ALWAYS AS (metadata->'$._aggregate_version') STORED NOT NULL
);

How do I change aggregate_version to be a non-generated column? The column type should stay the same.


Solution

  • 13.1.8.3 ALTER TABLE and Generated Columns

    ...

    • Stored but not virtual generated columns can be altered to nongenerated columns. The stored generated values become the values of the nongenerated column.

    ...

    Try:

    ALTER TABLE `messages`
    MODIFY `aggregate_version` INT UNSIGNED NOT NULL;
    

    See db-fiddle.