【问题标题】:go-gorm how to express many2many with additional columnsgo-gorm 如何用附加列表达 many2many
【发布时间】:2017-10-05 01:36:33
【问题描述】:

我想在 GORM 中表达以下表格:

CREATE TABLE indexes (
    id INTEGER PRIMARY KEY,
    name VARCHAR
)
CREATE TABLE services (
    id INTEGER PRIMARY KEY,
    name VARCHAR
)
CREATE TABLE index_service (
    index_id INTEGER REFERENCES indexes(id),
    service_id INTEGER REFERENCES services(id),
    write_active INTEGER,
    PRIMARY KEY (index_id, service_id)
)

阅读有关堆栈溢出的文档和问题后。我仍然找不到关于如何在 GORM 的 DSL 中表达附加列 write_active 的答案

到目前为止我得到的是

type Index struct {
   ID        unit `json:"id" gorm:"primary_key"`
   Name string    `json:"name" gorm:"not null"`
}

type Service struct {
   ID        unit `json:"id" gorm:"primary_key"`
   Name string    `json:"name" gorm:"not null"`
}

但是,我不知道如何写复合表。

【问题讨论】:

    标签: go-gorm


    【解决方案1】:

    你需要像这样创建额外的模型:

    package database
    
    type IndexService struct {
      WriteActive bool `gorm:"not null,DEFAULT false"`
    }
    
    

    【讨论】:

      猜你喜欢
      • 2019-12-30
      • 2017-06-22
      • 2020-11-20
      • 2020-12-08
      • 2023-03-04
      • 2021-02-28
      • 2020-04-11
      • 2020-02-05
      • 1970-01-01
      相关资源
      最近更新 更多