Jackyjjc
Jackyjjc

Reputation: 552

go-gorm how to express many2many with additional columns

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.

Upvotes: 5

Views: 1705

Answers (1)

jsina
jsina

Reputation: 4849

you need to create extra model like this:

package database

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

Upvotes: 2

Related Questions