mysqlmysql-8.0mysql-json

The value specified for generated column [whatever] is not allowed


I have a table - Lead.Metadata - with a column named meta that's of the JSON type and a generated column:

cycle VARCHAR(255) AS (JSON_UNQUOTE(meta->"$.cycle"))

The problem is that when I do UPDATE Lead.Metadata SET cycle = 'MONTHLY' I get this error:

Error Code: 3105. The value specified for generated column 'cycle' in table 'Metadata' is not allowed.

I don't understand. cycle is a VARCHAR(255) and 'MONTHLY' should fit in that.

Any ideas?

(also, fwiw, the codebase is a legacy codebase; the database structure is suboptimal but fixing that is a larger project)


Solution

  • Your column cycle is generated column, can not be updated directly.
    Values of a generated column are computed from an expression included in the column definition.
    Shortly: generated column is an expression.

    Update main column, then this column recalculated automatically.

    select id,cast(meta as json)meta,cycle 
    from Metadata;
    
    id meta cycle
    1 {"idx": "12", "cycle": "cycle1"} cycle1
    UPDATE Metadata 
      SET meta=json_set(meta,'$.cycle','MONTHLY')
    
    select id,cast(meta as json)meta,cycle 
    from Metadata;
    
    id meta cycle
    1 {"idx": "12", "cycle": "MONTHLY"} MONTHLY

    fiddle