Mysql set default value to a json type column Mysql set default value to a json type column sql sql

Mysql set default value to a json type column


The documentation says:

The BLOB, TEXT, GEOMETRY, and JSON data types can be assigned a default value only if the value is written as an expression, even if the expression value is a literal.

You can make your default an expression by surrounding the litteral value with parentheses:

ALTER TABLE templates CHANGE COLUMN values JSON NOT NULL DEFAULT ('{}') ;

Or:

ALTER TABLE templates CHANGE COLUMN values JSON NOT NULL DEFAULT (JSON_OBJECT()) ;


MySql syntax is a bit different than Oracle/Postgres, hence to make say JSON_Array as default, the query would be -

ALTER TABLE table_name ALTER column_name SET DEFAULT (JSON_ARRAY());

Further reference here