Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set a PostgreSQL function as default value in GORM?

I want something like:

type User struct {
    ID          int     `sql:"default:<myfunction>"`
}

Is this possible with GORM?

like image 956
Ben Nichols Avatar asked Nov 06 '15 18:11

Ben Nichols


People also ask

What is Postgres default value?

If no default value is declared explicitly, the default value is the null value. This usually makes sense because a null value can be considered to represent unknown data.

How do I find the default value of a column in postgresql?

you can just type " \d table_name" command , then It will displays some information about the table, such as the default value of a column. \d will show the default values of a column .

Is Gorm case sensitive?

Tags are optional to use when declaring models, GORM supports the following tags: Tags are case insensitive, however camelCase is preferred.


1 Answers

Have you tried it? You can do

time.Time `sql:"DEFAULT:current_timestamp"`

and it will use the "current_timestamp" function. If you want the default to be the string current_timestamp, you would do

time.Time `sql:"DEFAULT:'current_timestamp'"`

So, in short, yes, it is possible. You would just do:

type User struct {
    ID          int     `sql:"DEFAULT:myfunction"`
}
like image 120
dave Avatar answered Oct 03 '22 10:10

dave