how to define list of string on gorm model?

huangapple go评论113阅读模式
英文:

how to define list of string on gorm model?

问题

这是我的模型

  1. type Ticket struct {
  2. gorm.Model
  3. PassengerName string `json:"passenger_name"`
  4. Price uint64 `json:"price"`
  5. Seat pq.StringArray `gorm:"type:string[]" json:"seat"`
  6. }

gorm.io/driver/postgres@v1.3.1/migrator.go:118 错误: 类型 "string[]" 不存在 (SQLSTATE 42704)

英文:

this is my model

  1. type Ticket struct {
  2. gorm.Model
  3. PassengerName string `json:"passenger_name"`
  4. Price uint64 `json:"price"`
  5. Seat pq.StringArray `gorm:"type:string[]" json:"seat"`
  6. }

gorm.io/driver/postgres@v1.3.1/migrator.go:118 ERROR: type "string[]" does not exist (SQLSTATE 42704)

答案1

得分: 2

在PostgreSQL中没有字符串数据类型。将"string[]"更改为"text[]"。

英文:

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

答案2

得分: 1

这不是一个好的方法。你应该为它创建一个单独的表格。

票务表格:

  1. type Ticket struct {
  2. gorm.Model
  3. PassengerName string `json:"passenger_name"`
  4. Price uint64 `json:"price"`
  5. Seat []Seat `json:"seat" gorm:"foreignKey:SeatId"`
  6. }

座位表格:

  1. type Seat struct {
  2. gorm.Model
  3. SeatId serial `json:"seat_id"`
  4. Seat string `json:"seat"`
  5. }
英文:

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

Ticket Table:

  1. type Ticket struct {
  2. gorm.Model
  3. PassengerName string `json:"passenger_name"`
  4. Price uint64 `json:"price"`
  5. Seat []Seat `json:"seat" gorm:"foreignKey:SeatId"` }

Seat Table:

  1. type Seat struct {
  2. gorm.Modal
  3. SeatId serial `json:seat_id`
  4. Seat string `json:"seat"`}

huangapple
  • 本文由 发表于 2022年3月22日 11:34:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/71566625.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定