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

283 views Asked by At

why after I created a new table using go-pg, and found that the name of the new table changed? for example,the struct name is "story" and it became "stories" in pg.

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. Can someone explain to me why an 's' was added to the table name?

1

There are 1 answers

0
Pablo On

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. You can override the default table name and alias using tableName field:

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