猿问

如何在 gorm 模型上定义字符串列表?

这是我的模特


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/postgres@v1.3.1/migrator.go:118 错误:类型“string[]”不存在(SQLSTATE 42704)


幕布斯7119047
浏览 100回答 2
2回答

千巷猫影

postgre 中没有字符串数据类型。将字符串 [] 更改为文本 []

慕娘9325324

这不是一个好方法。你应该为它做一个单独的表门票表:type Ticket struct {    gorm.Model    PassengerName string         `json:"passenger_name"`    Price         uint64         `json:"price"`    Seat          []Seat         `json:"seat" gorm:"foreignKey:SeatId"` }座位表: type Seat struct {    gorm.Modal    SeatId  serial `json:seat_id`    Seat    string `json:"seat"`}
随时随地看视频慕课网APP

相关分类

Go
我要回答