GORM Playground Link
https://github.com/go-gorm/playground/pull/599
Description
I have a simply master/detail structure:
type Device struct {
ID int `gorm:"primarykey"`
Class Class
Type string
Details []DeviceDetail `gorm:"constraint:OnUpdate:CASCADE,OnDelete:CASCADE"`
}
type DeviceDetail struct {
DeviceID int `gorm:"index:idx_unqiue"`
Key string `gorm:"index:idx_unqiue"`
Value string
}
It is not possible to delete device_details due to:
2023/05/21 15:11:24 /Users/andig/htdocs/gorm-playground/main_test.go:49 WHERE conditions required
[0.004ms] [rows:0] DELETE FROM `device_details`
Comment From: github-actions[bot]
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.io ✨ Search Before Asking ✨
Comment From: andig
I think the problem here is the Delete signature:
Delete deletes value matching given conditions. If value contains primary key it is included in the conditions.
Hence, non-zero properties of the value parameter are apperently ignored. Fixed by changing:
tx.Delete(DeviceDetail{DeviceID: row.ID})
to
tx.Delete(new(DeviceDetail), DeviceDetail{DeviceID: row.ID})
It is not immediately obvious why it's designed this way.