Home > Software design >  how to define list of string on gorm model?
how to define list of string on gorm model?

Time:03-22

this is my model

type Ticket struct {
    gorm.Model

    PassengerName string         `json:"passenger_name"`
    Price         uint64         `json:"price"`
    Seat          pq.StringArray `gorm:"type:string[]" json:"seat"`
}

gorm.io/driver/[email protected]/migrator.go:118 ERROR: type "string[]" does not exist (SQLSTATE 42704)

CodePudding user response:

There’s no string data type in postgre. Change string[] to text[]

CodePudding user response:

It's not a good approach. You should make a separate table for it

Ticket Table:

type Ticket struct {
    gorm.Model

    PassengerName string         `json:"passenger_name"`
    Price         uint64         `json:"price"`
    Seat          []Seat         `json:"seat" gorm:"foreignKey:SeatId"` }

Seat Table:

 type Seat struct {
    gorm.Modal

    SeatId  serial `json:seat_id`
    Seat    string `json:"seat"`}
  • Related