to set singular name for a table in gorm

Yash Goel picture Yash Goel · Jun 16, 2017 · Viewed 12.9k times · Source
type user struct {
    ID       int
    Username string `gorm:"size:255"`
    Name     string `gorm:"size:255"`
}

I want to create a table 'user' using this model. But the table name automatically set to 'users'. I know it is gorm's default behaviour. But I want the table name to be 'user'.

Answer

bayrinat picture bayrinat · Jun 16, 2017

Set method TableName for your struct.

func (user) TableName() string {
    return "user"
}

Link: https://gorm.io/docs/models.html#conventions