简体   繁体   English

为什么我使用 go-pg 创建了一个新表后,发现新表的名称发生了变化?

[英]why after I created a new table using go-pg, and found that the name of the new table changed?

why after I created a new table using go-pg, and found that the name of the new table changed?为什么我使用 go-pg 创建了一个新表后,发现新表的名称发生了变化? for example,the struct name is "story" and it became "stories" in pg.例如,结构名称是“story”,它在 pg 中变成了“stories”。

1

type Newtb struct {
  Id     int64
  Name   string
  Emails []string
}



func createTest(db *pg.DB) error {
  err := db.Model((*Newtb)(nil)).CreateTable(&orm.CreateTableOptions{
    IfNotExists: true,
  })
  if err != nil {
    log.Fatal(err)
    return err
  }
  return nil
}

enter image description here在此处输入图像描述

My struct name is "Newtb" and it turned "newtbs" in postgreSQL.我的结构名称是“Newtb”,它在 postgreSQL 中变成了“newtbs”。 Can someone explain to me why an 's' was added to the table name?有人可以向我解释为什么在表名中添加了“s”吗?

Table name and alias are automatically derived from the struct name by underscoring it.表名和别名通过下划线自动从结构名派生而来。 Table name is also pluralized, for example struct Genre gets table name genres and alias genre.表名也是复数形式,例如 struct Genre 获取表名流派和别名流派。 You can override the default table name and alias using tableName field:您可以使用 tableName 字段覆盖默认表名和别名:

    type Genre struct {
    tableName struct{} `pg:"genres,alias:g"`
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM