go-gorm how to express many2many with additional columns

岁酱吖の 提交于 2020-01-14 13:32:18

问题


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)
)

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.


回答1:


you need to create extra model like this:

package database

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



来源:https://stackoverflow.com/questions/46576571/go-gorm-how-to-express-many2many-with-additional-columns

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!