Set unique name for table in gorm

type user struct {
    ID       int
    Username string `gorm:"size:255"`
    Name     string `gorm:"size:255"`
}

      

I want to create a "user" of a table using this model. But the table name is automatically set to "users". I know this is the default behavior for gorm. But I want the table name to be "user".

+3


source to share


2 answers


Set a method TableName

for your structure.

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

      



Link: http://jinzhu.me/gorm/models.html#conventions

+3


source


db.SingularTable(true)

      



Gorm has a built-in method for this, but it will be set globally for the entire table.

+1


source







All Articles