mirror of https://github.com/go-gorm/gorm.git
Fix Association.Replace() error returning (#3766)
* Fix Association.Replace() error returning * Fallback to gorm.Model at TestAssociationNotNullClear()
This commit is contained in:
parent
e7f45d5b01
commit
d66af581b4
|
@ -118,7 +118,7 @@ func (association *Association) Replace(values ...interface{}) error {
|
||||||
|
|
||||||
if _, pvs := schema.GetIdentityFieldValuesMap(reflectValue, primaryFields); len(pvs) > 0 {
|
if _, pvs := schema.GetIdentityFieldValuesMap(reflectValue, primaryFields); len(pvs) > 0 {
|
||||||
column, values := schema.ToQueryValues(rel.FieldSchema.Table, foreignKeys, pvs)
|
column, values := schema.ToQueryValues(rel.FieldSchema.Table, foreignKeys, pvs)
|
||||||
tx.Where(clause.IN{Column: column, Values: values}).UpdateColumns(updateMap)
|
association.Error = tx.Where(clause.IN{Column: column, Values: values}).UpdateColumns(updateMap).Error
|
||||||
}
|
}
|
||||||
case schema.Many2Many:
|
case schema.Many2Many:
|
||||||
var (
|
var (
|
||||||
|
@ -154,7 +154,7 @@ func (association *Association) Replace(values ...interface{}) error {
|
||||||
tx.Where(clause.Not(clause.IN{Column: relColumn, Values: relValues}))
|
tx.Where(clause.Not(clause.IN{Column: relColumn, Values: relValues}))
|
||||||
}
|
}
|
||||||
|
|
||||||
tx.Delete(modelValue)
|
association.Error = tx.Delete(modelValue).Error
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return association.Error
|
return association.Error
|
||||||
|
|
|
@ -3,6 +3,7 @@ package tests_test
|
||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"gorm.io/gorm"
|
||||||
. "gorm.io/gorm/utils/tests"
|
. "gorm.io/gorm/utils/tests"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -32,6 +33,41 @@ func TestInvalidAssociation(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestAssociationNotNullClear(t *testing.T) {
|
||||||
|
type Profile struct {
|
||||||
|
gorm.Model
|
||||||
|
Number string
|
||||||
|
MemberID uint `gorm:"not null"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type Member struct {
|
||||||
|
gorm.Model
|
||||||
|
Profiles []Profile
|
||||||
|
}
|
||||||
|
|
||||||
|
DB.Migrator().DropTable(&Member{}, &Profile{})
|
||||||
|
|
||||||
|
if err := DB.AutoMigrate(&Member{}, &Profile{}); err != nil {
|
||||||
|
t.Fatalf("Failed to migrate, got error: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
member := &Member{
|
||||||
|
Profiles: []Profile{{
|
||||||
|
Number: "1",
|
||||||
|
}, {
|
||||||
|
Number: "2",
|
||||||
|
}},
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := DB.Create(&member).Error; err != nil {
|
||||||
|
t.Fatalf("Failed to create test data, got error: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := DB.Model(member).Association("Profiles").Clear(); err == nil {
|
||||||
|
t.Fatalf("No error occured during clearind not null association")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestForeignKeyConstraints(t *testing.T) {
|
func TestForeignKeyConstraints(t *testing.T) {
|
||||||
type Profile struct {
|
type Profile struct {
|
||||||
ID uint
|
ID uint
|
||||||
|
|
Loading…
Reference in New Issue