mirror of https://github.com/go-gorm/gorm.git
fix: association without pks (#5779)
This commit is contained in:
parent
2a788fb20c
commit
186e8a9e14
|
@ -208,7 +208,10 @@ func SaveAfterAssociations(create bool) func(db *gorm.DB) {
|
||||||
|
|
||||||
cacheKey := utils.ToStringKey(relPrimaryValues...)
|
cacheKey := utils.ToStringKey(relPrimaryValues...)
|
||||||
if len(relPrimaryValues) != len(rel.FieldSchema.PrimaryFields) || !identityMap[cacheKey] {
|
if len(relPrimaryValues) != len(rel.FieldSchema.PrimaryFields) || !identityMap[cacheKey] {
|
||||||
|
if cacheKey != "" { // has primary fields
|
||||||
identityMap[cacheKey] = true
|
identityMap[cacheKey] = true
|
||||||
|
}
|
||||||
|
|
||||||
if isPtr {
|
if isPtr {
|
||||||
elems = reflect.Append(elems, elem)
|
elems = reflect.Append(elems, elem)
|
||||||
} else {
|
} else {
|
||||||
|
@ -294,7 +297,10 @@ func SaveAfterAssociations(create bool) func(db *gorm.DB) {
|
||||||
|
|
||||||
cacheKey := utils.ToStringKey(relPrimaryValues...)
|
cacheKey := utils.ToStringKey(relPrimaryValues...)
|
||||||
if len(relPrimaryValues) != len(rel.FieldSchema.PrimaryFields) || !identityMap[cacheKey] {
|
if len(relPrimaryValues) != len(rel.FieldSchema.PrimaryFields) || !identityMap[cacheKey] {
|
||||||
|
if cacheKey != "" { // has primary fields
|
||||||
identityMap[cacheKey] = true
|
identityMap[cacheKey] = true
|
||||||
|
}
|
||||||
|
|
||||||
distinctElems = reflect.Append(distinctElems, elem)
|
distinctElems = reflect.Append(distinctElems, elem)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -348,3 +348,45 @@ func TestAssociationEmptyQueryClause(t *testing.T) {
|
||||||
AssertEqual(t, len(orgs), 0)
|
AssertEqual(t, len(orgs), 0)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type AssociationEmptyUser struct {
|
||||||
|
ID uint
|
||||||
|
Name string
|
||||||
|
Pets []AssociationEmptyPet
|
||||||
|
}
|
||||||
|
|
||||||
|
type AssociationEmptyPet struct {
|
||||||
|
AssociationEmptyUserID *uint `gorm:"uniqueIndex:uniq_user_id_name"`
|
||||||
|
Name string `gorm:"uniqueIndex:uniq_user_id_name;size:256"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestAssociationEmptyPrimaryKey(t *testing.T) {
|
||||||
|
if DB.Dialector.Name() != "mysql" {
|
||||||
|
t.Skip()
|
||||||
|
}
|
||||||
|
DB.Migrator().DropTable(&AssociationEmptyUser{}, &AssociationEmptyPet{})
|
||||||
|
DB.AutoMigrate(&AssociationEmptyUser{}, &AssociationEmptyPet{})
|
||||||
|
|
||||||
|
id := uint(100)
|
||||||
|
user := AssociationEmptyUser{
|
||||||
|
ID: id,
|
||||||
|
Name: "jinzhu",
|
||||||
|
Pets: []AssociationEmptyPet{
|
||||||
|
{AssociationEmptyUserID: &id, Name: "bar"},
|
||||||
|
{AssociationEmptyUserID: &id, Name: "foo"},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
err := DB.Session(&gorm.Session{FullSaveAssociations: true}).Create(&user).Error
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Failed to create, got error: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
var result AssociationEmptyUser
|
||||||
|
err = DB.Preload("Pets").First(&result, &id).Error
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Failed to find, got error: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
AssertEqual(t, result, user)
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue