go-gorm如何用额外的列来表达many2many [英] go-gorm how to express many2many with additional columns

查看:153
本文介绍了go-gorm如何用额外的列来表达many2many的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to express the following tables in 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的答案

After reading through documentations and questions on stack overflow. I still cannot find an answer on how to express the additional column write_active in GORM's DSL

到目前为止,我得到的是

What I got so far is

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"`
}

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

However, I do not know how to write the composite table.

推荐答案

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

you need to create extra model like this:

package database

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

这篇关于go-gorm如何用额外的列来表达many2many的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆