Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

go-gorm how to express many2many with additional columns

Tags:

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

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.

like image 629
Jackyjjc Avatar asked Oct 29 '22 01:10

Jackyjjc


1 Answers

you need to create extra model like this:

package database

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

like image 190
jsina Avatar answered Jan 02 '23 20:01

jsina