Test ManyToMany relations with multi primary keys

This commit is contained in:
Jinzhu 2015-04-10 16:55:53 +08:00
parent 67266ebdb3
commit 1eb1ed091f
4 changed files with 76 additions and 14 deletions

View File

@ -40,6 +40,7 @@ func (association *Association) Append(values ...interface{}) *Association {
association.setErr(errors.New("invalid association type")) association.setErr(errors.New("invalid association type"))
} }
} }
scope.Search.Select(association.Column)
scope.callCallbacks(scope.db.parent.callback.updates) scope.callCallbacks(scope.db.parent.callback.updates)
return association.setErr(scope.db.Error) return association.setErr(scope.db.Error)
} }

View File

@ -64,6 +64,7 @@ func Update(scope *Scope) {
} }
} }
if len(sqls) > 0 {
scope.Raw(fmt.Sprintf( scope.Raw(fmt.Sprintf(
"UPDATE %v SET %v %v", "UPDATE %v SET %v %v",
scope.QuotedTableName(), scope.QuotedTableName(),
@ -73,6 +74,7 @@ func Update(scope *Scope) {
scope.Exec() scope.Exec()
} }
} }
}
func AfterUpdate(scope *Scope) { func AfterUpdate(scope *Scope) {
if _, ok := scope.Get("gorm:update_column"); !ok { if _, ok := scope.Get("gorm:update_column"); !ok {

View File

@ -36,26 +36,39 @@ func (s *JoinTableHandler) Setup(relationship *Relationship, tableName string, s
s.Source = JoinTableSource{ModelType: source} s.Source = JoinTableSource{ModelType: source}
sourceScope := &Scope{Value: reflect.New(source).Interface()} sourceScope := &Scope{Value: reflect.New(source).Interface()}
for _, primaryField := range sourceScope.GetModelStruct().PrimaryFields { sourcePrimaryFields := sourceScope.GetModelStruct().PrimaryFields
for _, primaryField := range sourcePrimaryFields {
if relationship.ForeignDBName == "" { if relationship.ForeignDBName == "" {
relationship.ForeignFieldName = source.Name() + primaryField.Name relationship.ForeignFieldName = source.Name() + primaryField.Name
relationship.ForeignDBName = ToDBName(relationship.ForeignFieldName) relationship.ForeignDBName = ToDBName(relationship.ForeignFieldName)
} }
var dbName string
if len(sourcePrimaryFields) == 1 || primaryField.DBName == "id" {
dbName = relationship.ForeignDBName
} else {
dbName = ToDBName(source.Name() + primaryField.Name)
}
s.Source.ForeignKeys = append(s.Source.ForeignKeys, JoinTableForeignKey{ s.Source.ForeignKeys = append(s.Source.ForeignKeys, JoinTableForeignKey{
DBName: relationship.ForeignDBName, DBName: dbName,
AssociationDBName: primaryField.DBName, AssociationDBName: primaryField.DBName,
}) })
} }
s.Destination = JoinTableSource{ModelType: destination} s.Destination = JoinTableSource{ModelType: destination}
destinationScope := &Scope{Value: reflect.New(destination).Interface()} destinationScope := &Scope{Value: reflect.New(destination).Interface()}
for _, primaryField := range destinationScope.GetModelStruct().PrimaryFields { destinationPrimaryFields := destinationScope.GetModelStruct().PrimaryFields
if relationship.AssociationForeignDBName == "" { for _, primaryField := range destinationPrimaryFields {
relationship.AssociationForeignFieldName = destination.Name() + primaryField.Name var dbName string
relationship.AssociationForeignDBName = ToDBName(relationship.AssociationForeignFieldName) if len(sourcePrimaryFields) == 1 || primaryField.DBName == "id" {
dbName = relationship.AssociationForeignDBName
} else {
dbName = ToDBName(destinationScope.GetModelStruct().ModelType.Name() + primaryField.Name)
} }
s.Destination.ForeignKeys = append(s.Destination.ForeignKeys, JoinTableForeignKey{ s.Destination.ForeignKeys = append(s.Destination.ForeignKeys, JoinTableForeignKey{
DBName: relationship.AssociationForeignDBName, DBName: dbName,
AssociationDBName: primaryField.DBName, AssociationDBName: primaryField.DBName,
}) })
} }

View File

@ -0,0 +1,46 @@
package gorm_test
import (
"fmt"
"os"
"testing"
)
type Blog struct {
ID uint `gorm:"primary_key"`
Locale string `gorm:"primary_key"`
Subject string
Body string
Tags []Tag `gorm:"many2many:blog_tags;"`
}
type Tag struct {
ID uint `gorm:"primary_key"`
Locale string `gorm:"primary_key"`
Value string
}
func TestManyToManyWithMultiPrimaryKeys(t *testing.T) {
if dialect := os.Getenv("GORM_DIALECT"); dialect != "sqlite" {
DB.Exec(fmt.Sprintf("drop table blog_tags;"))
DB.AutoMigrate(&Blog{}, &Tag{})
blog := Blog{
Locale: "ZH",
Subject: "subject",
Body: "body",
Tags: []Tag{
{Locale: "ZH", Value: "tag1"},
{Locale: "ZH", Value: "tag2"},
},
}
DB.Save(&blog)
DB.Model(&blog).Association("Tags").Append([]Tag{{Locale: "ZH", Value: "tag3"}})
var tags []Tag
DB.Model(&blog).Related(&tags, "Tags")
if len(tags) != 3 {
t.Errorf("should found 3 tags with blog")
}
}
}