使用GORM,是否可以将默认值设置为PostgreSQL函数? [英] Using GORM, is there a way for me to set a default value as a PostgreSQL function?

查看:66
本文介绍了使用GORM,是否可以将默认值设置为PostgreSQL函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要类似的东西:

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

GORM有可能吗?

推荐答案

您尝试过吗?你可以做

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

,它将使用"current_timestamp"功能.如果希望默认值为字符串 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"`
}

这篇关于使用GORM,是否可以将默认值设置为PostgreSQL函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆