登录
首页 >  Golang >  Go问答

三个模型之间是否支持多对多关系?(戈尔姆)

来源:stackoverflow

时间:2024-02-10 10:36:23 290浏览 收藏

在IT行业这个发展更新速度很快的行业,只有不停止的学习,才不会被行业所淘汰。如果你是Golang学习者,那么本文《三个模型之间是否支持多对多关系?(戈尔姆)》就很适合你!本篇内容主要包括##content_title##,希望对大家的知识积累有所帮助,助力实战开发!

问题内容

是否可以在 3 个模型之间使用多对多?

我有 3 个模型想要加入桥接表“client_operator_role”

  1. 运算符
type operator struct {
    gorm.model
    title             string            `gorm:"unique;not null" validate:"required,min=1,max=100"`
    email             string            `gorm:"not null;unique" validate:"required,email"`
    mobile            string            `gorm:"not null" validate:"required,min=7,max=15,numeric"`
    last_online       time.time         `gorm:"default:null" validate:"omitempty"`
    last_ip           string            `gorm:"default:null" validate:"omitempty,ip"`
    clients           []*client         `gorm:"many2many:client_operator_role;"`
    cli_ids           []string          `gorm:"-:all" validate:"omitempty,dive,numeric"` // placeholder field, wont be part of table
    goadmincustomuser goadmincustomuser `validate:"omitempty,dive"`
}
  1. 客户端
type client struct {
    gorm.model
    name        string      `gorm:"unique;not null" validate:"required,min=1,max=30"`
    kyc_status  string      `gorm:"not null" validate:"required,min=1,max=30"`
    kyc_remarks string      `gorm:"default:null" validate:"omitempty,min=0,max=200"`
    operators   []*operator `gorm:"many2many:client_operator_role;"`
    op_ids      []string    `gorm:"-:all" validate:"omitempty,dive,numeric"` // placeholder field, wont be part of table
    users       []user
}
  1. 角色
type GoadminRole struct {
    ID        uint `gorm:"primaryKey"`
    Name      string
    Slug      string
    CreatedAt time.Time
    UpdatedAt time.Time
    Operators []*Operator `gorm:"many2many:client_operator_role;"`
}

gorm 文档声明 many2many 仅适用于 2 个模型。有没有解决方法可以将这三个结合起来?


正确答案


您可以为此使用自定义联接表。 https://gorm.io/docs/many_to_many.html#Customize-JoinTable

创建桥接表

type connection struct {
    gorm.model

    roleid     int `gorm:"primarykey"`
    clientid   int `gorm:"primarykey"`
    operatorid int `gorm:"primarykey"`
}

添加表对每个模型的引用

type operator struct {
    // ..
    clients []*client `gorm:"many2many:connections;"`
    roles   []*role   `gorm:"many2many:connections;"`
}

type client struct {
    // ..
    operators []*operator `gorm:"many2many:connections;"`
    roles     []*role     `gorm:"many2many:connections;"`
}

type role struct {
    // ..
    clients   []*client   `gorm:"many2many:connections;"`
    operators []*operator `gorm:"many2many:connections;"`
}

您应该使用 setupjointable 函数。 类似的东西对我有用:

// do not forget to check for errors
db.SetupJoinTable(&models.Operator{}, "Roles", &models.Connection{})
db.SetupJoinTable(&models.Operator{}, "Clients", &models.Connection{})

db.SetupJoinTable(&models.Client{}, "Roles", &models.Connection{})
db.SetupJoinTable(&models.Client{}, "Operators", &models.Connection{})

db.SetupJoinTable(&models.Role{}, "Clients", &models.Connection{})
db.SetupJoinTable(&models.Role{}, "Operators", &models.Connection{})

本篇关于《三个模型之间是否支持多对多关系?(戈尔姆)》的介绍就到此结束啦,但是学无止境,想要了解学习更多关于Golang的相关知识,请关注golang学习网公众号!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>