是否可以在 3 个模型之间使用多对多? (戈尔姆)

发布于 2025-01-20 21:51:57 字数 1797 浏览 1 评论 0原文

是否可以在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 docs state Mose2many仅适用于2个模型。有能力加入这三个吗?

Is it possible to use many to many between 3 models?

I have 3 models I want to join into a bridge table "client_operator_role"

  1. Operator
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. Client
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. Role
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 docs state many2many is only for 2 models. Is there a workaround to join these three?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

最初的梦 2025-01-27 21:51:57

您可以为此使用自定义联接表。 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{})

You can use custom join table for this. https://gorm.io/docs/many_to_many.html#Customize-JoinTable

Create bridge table

type Connection struct {
    gorm.Model

    RoleID     int `gorm:"primaryKey"`
    ClientID   int `gorm:"primaryKey"`
    OperatorID int `gorm:"primaryKey"`
}

Add tables' references to each model

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

Then you should use SetupJoinTable func.
Something like that works for me:

// 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{})
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文