[MDEV-23531] Allow functions for column default values Created: 2020-08-21  Updated: 2023-06-12  Resolved: 2020-08-31

Status: Closed
Project: MariaDB Server
Component/s: Data Definition - Create Table
Fix Version/s: 10.2.1

Type: Task Priority: Major
Reporter: Manjot Singh (Inactive) Assignee: Sergei Golubchik
Resolution: Fixed Votes: 0
Labels: None


 Description   

With datetime we can do:

CREATE TABLE t1 (
updatedTime datetime DEFAULT CURRENT_TIMESTAMP() ON UPDATE CURRENT_TIMESTAMP()
);

We should be able to use any function as a default on any data type, like this:

CREATE TABLE t1 (
myCol longtext DEFAULT JSON_OBJECT('a',1,'b',2),
lottoNumber int(11) DEFAULT myUDF(),
updatedBy varchar(255) DEFAULT CURRENT_USER() ON UPDATE CURRENT_USER()
);



 Comments   
Comment by Sergei Golubchik [ 2020-08-30 ]

See https://mariadb.com/kb/en/create-table/#default-column-option

Comment by Manjot Singh (Inactive) [ 2020-08-31 ]

Thanks for pointing out the documentation I missed. But this still only works on only blob and text columns.

This ticket is for any column type. In my example, updatedBy is currentUser.. I wouldn't want to take the overhead of text for something that is usually less than 50 characters.

Also, ON UPDATE does not work with text.

MariaDB [test]> CREATE TABLE t1 (
    -> lottoNumber int(11) DEFAULT myUDF(),
    -> updatedBy varchar(255) DEFAULT CURRENT_USER() ON UPDATE CURRENT_USER()
    -> );
ERROR 1901 (HY000): Function or expression '`myUDF`()' cannot be used in the DEFAULT clause of `lottoNumber`
MariaDB [test]> CREATE TABLE t1 ( myCol longtext DEFAULT JSON_OBJECT('a',1,'b',2));
Query OK, 0 rows affected (0.015 sec)
MariaDB [test]> CREATE TABLE t1 ( updatedBy text DEFAULT CURRENT_USER() ON UPDATE CURRENT_USER() );
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'CURRENT_USER() )' at line 1
MariaDB [test]> CREATE TABLE t1 ( updatedBy text DEFAULT CURRENT_USER() );
Query OK, 0 rows affected (0.020 sec)

Comment by Sergei Golubchik [ 2020-08-31 ]

ON UPDATE is a non-standard historical extension for TIMESTAMP columns. Use triggers instead.
Otherwise expressions can be used in DEFAULT for any column time. See https://mariadb.com/kb/en/create-table/#default-column-option

Comment by Manjot Singh (Inactive) [ 2020-08-31 ]

I just showed you that it doesn't work for int or varchar. .. it doesnt work for any column type.

Comment by Oded Arbel [ 2023-06-12 ]

The documentation for "DEFAULT Column Option" seems to suggest that as of MariaDB 10.2.1, one can create tables with a DEFAULT option set to a function or and expression, which I understand to mean - it is possible to create a table with a column default value calling a user function.

If I try to do that, I get something like this:

Function or expression '`my_function`()' cannot be used in the DEFAULT clause of `my_column` 

I think this is what the OP was talking about.

Generated at Thu Feb 08 09:23:06 UTC 2024 using Jira 8.20.16#820016-sha1:9d11dbea5f4be3d4cc21f03a88dd11d8c8687422.