How to write comment for a table using gorm?

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

How to write comment for a table using gorm?

问题

如何使用gorm为表编写注释(而不是字段)?

是否有类似于TableName() string用于表名的方法,用于表注释?

type Human struct {
    ID int
}

func (h *Human) TableName() string {
    return "human_table"
}

func (h *Human) Comment() string {
    return "this is table for human"
}

func main() {
    //
    db.AutoMigrate(&Human{})
    //
}
  • 我正在使用PostgreSQL数据库。
英文:

How can I write comment for a table using gorm? (not for the field)

Is there any methods for the table comment like TableName() string for the table name?

type Human struct {
    ID int
}

func (h *Human) TableName() string {
    return "human_table"
}

func (h *Human) Comment() string {
    return "this is table for human"
}

func main() {
    //
    db.AutoMigrate(&Human{})
    //
}
  • I am using postgresql

答案1

得分: 1

在PostgreSQL中,我不太熟悉,但在MySQL中,你可以通过gorm:table_options来添加表注释,具体描述可以参考https://gorm.io/docs/migration.html#Auto-Migration:

if err := Db.Set("gorm:table_options", "ENGINE InnoDB COMMENT 'users'").
	AutoMigrate(&User{}); err != nil {
	panic(err)
}
if err := Db.Set("gorm:table_options", "ENGINE InnoDB COMMENT 'groups'").
	AutoMigrate(&Group{}); err != nil {
	panic(err)
}
英文:

Not familiar to PostgreSQL, but in MySQL, you can add table comment by gorm:table_options, as described in https://gorm.io/docs/migration.html#Auto-Migration:

if err := Db.Set("gorm:table_options", "ENGINE InnoDB COMMENT 'users'").
	AutoMigrate(&User{}); err != nil {
	panic(err)
}
if err := Db.Set("gorm:table_options", "ENGINE InnoDB COMMENT 'groups'").
	AutoMigrate(&Group{}); err != nil {
	panic(err)
}

huangapple
  • 本文由 发表于 2022年7月18日 14:37:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/73018164.html
匿名

发表评论

匿名网友

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

确定