'How can i add enum in gorm?
I am writing PostgreSQL table schema.
type TestTable struct {
ID int `gorm:"column:id;primaryKey;autoIncrement"`
CarType string `gorm:"column:car_type"`
}
So how can i add car types like "SEDAN", "HATCHBACK", "MINIVAN" as enum data type
Solution 1:[1]
Assuming you are using GORM. First in your database create a type.
CREATE TYPE car_type AS ENUM (
'SEDAN',
'HATCHBACK',
'MINIVAN');
Then you will need to define the following model:
type carType string
const (
SEDAN carType = "SEDAN"
HATCHBACK carType = "HATCHBACK"
MINIVAN carType = "MINIVAN"
)
func (ct *carType) Scan(value interface{}) error {
*ct = carType(value.([]byte))
return nil
}
func (ct carType) Value() (driver.Value, error) {
return string(ct), nil
}
type MyTable struct {
gorm.Model
CarType carType `sql:"car_type"`
}
func (MyTable) TableName() string {
return "my_table"
}
Solution 2:[2]
in order to extend Nick's answer I am adding the following for automation:
Assuming you have DBClient
struct, you can create a method that creates this car type:
func (psqlClient *DBClient) CreateCarTypeEnum() error {
result := psqlClient.db.Exec("SELECT 1 FROM pg_type WHERE typname = 'car_type';")
switch {
case result.RowsAffected == 0:
if err := psqlClient.db.Exec("CREATE TYPE car_type AS ENUM ('SEDAN', 'HATCHBACK', 'MINIVAN');").Error; err != nil {
log.Error().Err(err).Msg("Error creating car_type ENUM")
return err
}
return nil
case result.Error != nil:
return result.Error
default:
return nil
}
}
Solution 3:[3]
on a side note- if you decide to go with slightly different approach:
you can define your enums as int, and leverage iota
. then you can use code generator to create sql Scaner/Valuer
but also json/text representations.
for example:
https://github.com/dmarkham/enumer
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Nick |
Solution 2 | |
Solution 3 | mihwyz |