Fix omit associations, close #4161

This commit is contained in:
Jinzhu 2021-03-19 15:15:26 +08:00
parent e85b73e5a5
commit 220349ccf2
2 changed files with 26 additions and 1 deletions

View File

@ -377,7 +377,7 @@ func saveAssociations(db *gorm.DB, rel *schema.Relationship, values interface{},
if len(selects) > 0 {
tx = tx.Select(selects)
} else if len(selectColumns) > 0 && len(omits) == 0 {
} else if restricted && len(omits) == 0 {
tx = tx.Omit(clause.Associations)
}

View File

@ -398,6 +398,31 @@ func TestMultipleMany2Many(t *testing.T) {
)
}
func TestSelfReferentialMany2Many(t *testing.T) {
type User struct {
ID int32 `gorm:"primaryKey"`
Name string
CreatedBy int32
Creators []User `gorm:"foreignKey:CreatedBy"`
AnotherPro interface{} `gorm:"-"`
}
checkStructRelation(t, &User{}, Relation{
Name: "Creators", Type: schema.HasMany, Schema: "User", FieldSchema: "User",
References: []Reference{{"ID", "User", "CreatedBy", "User", "", true}},
})
user, err := schema.Parse(&User{}, &sync.Map{}, schema.NamingStrategy{})
if err != nil {
t.Fatalf("failed to parse schema")
}
relSchema := user.Relationships.Relations["Creators"].FieldSchema
if user != relSchema {
t.Fatalf("schema should be same, expects %p but got %p", user, relSchema)
}
}
type CreatedByModel struct {
CreatedByID uint
CreatedBy *CreatedUser