Using GORM, is there a way for me to set a default value as a PostgreSQL function?

Ben Nichols picture Ben Nichols · Nov 6, 2015 · Viewed 17k times · Source

I want something like:

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

Is this possible with GORM?

Answer

dave picture dave · Nov 6, 2015

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"`
}