如何删除关联(many2many)?

How to delete association (many2many)?

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

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_operators` 中删除 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() 也没有做任何事情。

发生这种情况是因为您将 &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)