英文:
How to set a PostgreSQL function as default value in GORM?
问题
我想要类似这样的代码:
type User struct {
ID int `sql:"default:<myfunction>"`
}
使用 GORM 可以实现这个吗?
英文:
I want something like:
type User struct {
ID int `sql:"default:<myfunction>"`
}
Is this possible with GORM?
答案1
得分: 13
你试过了吗?你可以这样做:
time.Time `sql:"DEFAULT:current_timestamp"`
它将使用"current_timestamp"函数作为默认值。如果你想要默认值是字符串current_timestamp
,你可以这样做:
time.Time `sql:"DEFAULT:'current_timestamp'"`
所以,简而言之,是可以的。你只需要这样做:
type User struct {
ID int `sql:"DEFAULT:myfunction"`
}
英文:
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"`
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论