如何删除关联(many2many)?

发布于 2025-01-16 04:23:05 字数 2033 浏览 1 评论 0原文

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

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] DELETE FROM `client_operators` WHERE client_id = 5 AND `client_operators`.`operator_id` = 1

OR

[2.000ms] [行数: 0] 从“client_operators”中删除“client_operators”。“client_id”= 5 并且`client_operators`.`operator_id` = 1

但目前是这样的:

[2.000ms] [rows:0] DELETE FROM `client_operators` WHERE client_id = 5 AND `client_operators`.`client_id` IN (NULL) AND `client_operators`.`operator_id` = 1

它添加了额外条件 " AND `client_operators`.`client_id` IN (NULL) "

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

Strange behaviour deleting association happened.
The query generated an extra condition which i did not add.

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)

I expect the deletion to be:

[2.000ms] [rows:0] DELETE FROM `client_operators` WHERE client_id = 5 AND `client_operators`.`operator_id` = 1

OR

[2.000ms] [rows:0] DELETE FROM `client_operators` WHERE `client_operators`.`client_id` = 5 AND `client_operators`.`operator_id` = 1

However it currently does:

[2.000ms] [rows:0] DELETE FROM `client_operators` WHERE client_id = 5 AND `client_operators`.`client_id` IN (NULL) AND `client_operators`.`operator_id` = 1

It adds the extra condition of " AND `client_operators`.`client_id` IN (NULL) "

I tried Association().Clear() and did not do anything too.

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

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

发布评论

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

评论(1

音盲 2025-01-23 04:23:05

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

查看 gorm 文档 您需要使用有效的 构建一个客户端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)

This is happening because you're passing &Client{} to Model.

Looking at gorm docs you need to construct a client with a valid Id first like this:

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)

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