Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Strange behaviour deleting associations #5180

Closed
Thambolo opened this issue Mar 22, 2022 · 4 comments · Fixed by #5187
Closed

Strange behaviour deleting associations #5180

Thambolo opened this issue Mar 22, 2022 · 4 comments · Fixed by #5187
Assignees
Labels

Comments

@Thambolo
Copy link

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) "

@github-actions github-actions bot added the type:missing reproduction steps missing reproduction steps label Mar 22, 2022
@github-actions
Copy link

The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 30 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.ioSearch Before Asking

@a631807682
Copy link
Member

This is because client_id could not be obtained. Association Model require primary key to delete.
Change DBconnection.Model(&Client{}) to DBconnection.Model(&client_query).

@github-actions
Copy link

The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 30 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.ioSearch Before Asking

@Thambolo
Copy link
Author

This is because client_id could not be obtained. Association Model require primary key to delete. Change DBconnection.Model(&Client{}) to DBconnection.Model(&client_query).

太好了! 谢谢你 @a631807682

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants