如何在CreateTables()中为列设置默认值?

huangapple go评论81阅读模式
英文:

How to set default value for a column on CreateTables()?

问题

你可以通过在结构体字段上使用db标签来设置默认值。在CreateTables()方法中,DbMap会根据结构体的定义创建表格,并将默认值应用于相应的列。

例如,假设你有一个名为User的结构体,其中包含一个名为Status的字段,你可以在该字段上添加db标签来设置默认值。示例代码如下:

type User struct {
    ID     int64  `db:"id"`
    Name   string `db:"name"`
    Status string `db:"status,default:'active'"`
}

在上述代码中,Status字段的db标签设置了默认值为'active'。当你调用CreateTables()方法时,DbMap会根据结构体定义创建表格,并将Status列的默认值设置为'active'

希望这可以帮助到你!如果你有任何其他问题,请随时问我。

英文:

I am using gorp, and I want to create a table with DbMap#CreateTables() by setting a default value for a column.

How can I achieve this?

答案1

得分: 0

这个功能的实现目前正在由项目的合作者进行审核。请查看相应的拉取请求

英文:

An implementation of this feature is currently being reviewed by the project's collaborators.
See the corresponding pull request.

huangapple
  • 本文由 发表于 2015年5月5日 12:31:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/30044331.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定