登录
首页 >  Golang >  Go问答

如何取消关联(many2many)?

来源:stackoverflow

时间:2024-02-10 15:27:23 314浏览 收藏

目前golang学习网上已经有很多关于Golang的文章了,自己在初次阅读这些文章中,也见识到了很多学习思路;那么本文《如何取消关联(many2many)?》,也希望能帮助到大家,如果阅读完后真的对你学习Golang有帮助,欢迎动动手指,评论留言并分享~

问题内容

发生删除关联的奇怪行为。 该查询生成了一个我没有添加的额外条件。

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_operators;"`
    Op_ids      []string    `gorm:"-:all" validate:"omitempty,dive,numeric"` // placeholder field, wont be part of table
}
type Operator struct {
    gorm.Model
    Title       string    `gorm:"unique;not null" validate:"required,min=1,max=100"`
    Email       string    `gorm:"not null" 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_operators;"`
    Cli_ids     []string  `gorm:"-:all" validate:"omitempty,dive,numeric"`
}

// find operators related to client
var client_query *Client
DBconnection.Where("id = ?", pk).Preload("Operators").First(&client_query)

// delete operators related to client
DBconnection.Model(&Client{}).Where("client_id = ?", pk).Association("Operators").Delete(&client_query.Operators)

我希望删除:

[2.000ms] [行:0] 从 `client_operators` 中删除,其中 client_id = 5 并且 `client_operators`.`operator_id` = 1

[2.000ms] [行:0] 从 `client_operators` 中删除,其中 `client_operators`.`client_id` = 5 且 `client_operators`.`operator_id` = 1

但是目前确实如此:

[2.000ms] [行:0] 从 client_id = 5 处的 `client_operators` 中删除 and `client_operators`.`client_id` in (null) and `client_operators `.`operator_id` = 1

它添加了额外条件“ and `client_operators`.`client_id` in (null)

我尝试了 association().clear() 但也没有做任何事情。


正确答案


发生这种情况是因为您将 &client{} 传递给 model

查看 gorm docs,您需要首先构建一个具有有效 id 的客户端,如下所示:

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_operators;"`
    Op_ids      []string    `gorm:"-:all" validate:"omitempty,dive,numeric"` // placeholder field, wont be part of table
}
type Operator struct {
    gorm.Model
    Title       string    `gorm:"unique;not null" validate:"required,min=1,max=100"`
    Email       string    `gorm:"not null" 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_operators;"`
    Cli_ids     []string  `gorm:"-:all" validate:"omitempty,dive,numeric"`
}

// find operators related to client
var client_query *Client
DBconnection.Where("id = ?", pk).Preload("Operators").First(&client_query)



// delete operators related to client
DBconnection.Model(&Client{ID: pk}).Association("Operators").Delete(&client_query.Operators)

今天关于《如何取消关联(many2many)?》的内容介绍就到此结束,如果有什么疑问或者建议,可以在golang学习网公众号下多多回复交流;文中若有不正之处,也希望回复留言以告知!

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