Skip to content

Commit

Permalink
Fix source type is incorrect error for embedded many to many relation…
Browse files Browse the repository at this point in the history
…ship
  • Loading branch information
jinzhu committed Jun 14, 2017
1 parent 9acaa33 commit eae7f6b
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 1 deletion.
4 changes: 3 additions & 1 deletion association.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,9 @@ func (association *Association) Count() int {
)
}

query.Model(fieldValue).Count(&count)
if err := query.Model(fieldValue).Count(&count).Error; err != nil {
association.Error = err
}
return count
}

Expand Down
2 changes: 2 additions & 0 deletions join_table_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ func (s *JoinTableHandler) Setup(relationship *Relationship, tableName string, s
s.TableName = tableName

s.Source = JoinTableSource{ModelType: source}
s.Source.ForeignKeys = []JoinTableForeignKey{}
for idx, dbName := range relationship.ForeignFieldNames {
s.Source.ForeignKeys = append(s.Source.ForeignKeys, JoinTableForeignKey{
DBName: relationship.ForeignDBNames[idx],
Expand All @@ -67,6 +68,7 @@ func (s *JoinTableHandler) Setup(relationship *Relationship, tableName string, s
}

s.Destination = JoinTableSource{ModelType: destination}
s.Destination.ForeignKeys = []JoinTableForeignKey{}
for idx, dbName := range relationship.AssociationForeignFieldNames {
s.Destination.ForeignKeys = append(s.Destination.ForeignKeys, JoinTableForeignKey{
DBName: relationship.AssociationForeignDBNames[idx],
Expand Down
35 changes: 35 additions & 0 deletions join_table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,38 @@ func TestJoinTable(t *testing.T) {
t.Errorf("Should deleted all addresses")
}
}

func TestEmbeddedMany2ManyRelationship(t *testing.T) {
type EmbeddedPerson struct {
ID int
Name string
Addresses []*Address `gorm:"many2many:person_addresses;"`
}

type NewPerson struct {
EmbeddedPerson
ExternalID uint
}
DB.Exec("drop table person_addresses;")
DB.AutoMigrate(&NewPerson{})

address1 := &Address{Address1: "address 1"}
address2 := &Address{Address1: "address 2"}
person := &NewPerson{ExternalID: 100, EmbeddedPerson: EmbeddedPerson{Name: "person", Addresses: []*Address{address1, address2}}}
if err := DB.Save(person).Error; err != nil {
t.Errorf("no error should return when save embedded many2many relationship, but got %v", err)
}

if err := DB.Model(person).Association("Addresses").Delete(address1).Error; err != nil {
t.Errorf("no error should return when delete embedded many2many relationship, but got %v", err)
}

association := DB.Model(person).Debug().Association("Addresses")
if count := association.Count(); count != 1 || association.Error != nil {
t.Errorf("Should found one address, but got %v, error is %v", count, association.Error)
}

if association.Clear(); association.Count() != 0 {
t.Errorf("Should deleted all addresses")
}
}
7 changes: 7 additions & 0 deletions model_struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,13 @@ func (scope *Scope) GetModelStruct() *ModelStruct {
subField.IsPrimaryKey = false
}
}

if subField.Relationship != nil && subField.Relationship.JoinTableHandler != nil {
if joinTableHandler, ok := subField.Relationship.JoinTableHandler.(*JoinTableHandler); ok {
joinTableHandler.Setup(subField.Relationship, joinTableHandler.TableName, reflectType, joinTableHandler.Destination.ModelType)
}
}

modelStruct.StructFields = append(modelStruct.StructFields, subField)
}
continue
Expand Down

0 comments on commit eae7f6b

Please sign in to comment.