Add *DB' param to JoinTableHandlerInterface.Setup' interface to preserve DB settings in intermediate setup scope.

Fixes previously non-functioning/broken use-case:

    - Using the `gorm:many2many' relations tag with `db.SingularTable(true)'
	  should always reference singular table names.
This commit is contained in:
Jay Taylor 2015-07-02 11:58:54 -07:00
parent 27a442b5ec
commit a9d11a9cc8
3 changed files with 5 additions and 4 deletions

View File

@ -8,7 +8,7 @@ import (
) )
type JoinTableHandlerInterface interface { type JoinTableHandlerInterface interface {
Setup(relationship *Relationship, tableName string, source reflect.Type, destination reflect.Type) Setup(db *DB, relationship *Relationship, tableName string, source reflect.Type, destination reflect.Type)
Table(db *DB) string Table(db *DB) string
Add(handler JoinTableHandlerInterface, db *DB, source interface{}, destination interface{}) error Add(handler JoinTableHandlerInterface, db *DB, source interface{}, destination interface{}) error
Delete(handler JoinTableHandlerInterface, db *DB, sources ...interface{}) error Delete(handler JoinTableHandlerInterface, db *DB, sources ...interface{}) error
@ -41,7 +41,7 @@ func (s *JoinTableHandler) DestinationForeignKeys() []JoinTableForeignKey {
return s.Destination.ForeignKeys return s.Destination.ForeignKeys
} }
func (s *JoinTableHandler) Setup(relationship *Relationship, tableName string, source reflect.Type, destination reflect.Type) { func (s *JoinTableHandler) Setup(db *DB, relationship *Relationship, tableName string, source reflect.Type, destination reflect.Type) {
s.TableName = tableName s.TableName = tableName
s.Source = JoinTableSource{ModelType: source} s.Source = JoinTableSource{ModelType: source}
@ -68,6 +68,7 @@ func (s *JoinTableHandler) Setup(relationship *Relationship, tableName string, s
s.Destination = JoinTableSource{ModelType: destination} s.Destination = JoinTableSource{ModelType: destination}
destinationScope := &Scope{Value: reflect.New(destination).Interface()} destinationScope := &Scope{Value: reflect.New(destination).Interface()}
destinationScope.db = db
destinationPrimaryFields := destinationScope.GetModelStruct().PrimaryFields destinationPrimaryFields := destinationScope.GetModelStruct().PrimaryFields
for _, primaryField := range destinationPrimaryFields { for _, primaryField := range destinationPrimaryFields {
var dbName string var dbName string

View File

@ -485,7 +485,7 @@ func (s *DB) SetJoinTableHandler(source interface{}, column string, handler Join
if many2many := parseTagSetting(field.Tag.Get("gorm"))["MANY2MANY"]; many2many != "" { if many2many := parseTagSetting(field.Tag.Get("gorm"))["MANY2MANY"]; many2many != "" {
source := (&Scope{Value: source}).GetModelStruct().ModelType source := (&Scope{Value: source}).GetModelStruct().ModelType
destination := (&Scope{Value: reflect.New(field.Struct.Type).Interface()}).GetModelStruct().ModelType destination := (&Scope{Value: reflect.New(field.Struct.Type).Interface()}).GetModelStruct().ModelType
handler.Setup(field.Relationship, many2many, source, destination) handler.Setup(s, field.Relationship, many2many, source, destination)
field.Relationship.JoinTableHandler = handler field.Relationship.JoinTableHandler = handler
if table := handler.Table(s); scope.Dialect().HasTable(scope, table) { if table := handler.Table(s); scope.Dialect().HasTable(scope, table) {
s.Table(table).AutoMigrate(handler) s.Table(table).AutoMigrate(handler)

View File

@ -229,7 +229,7 @@ func (scope *Scope) GetModelStruct() *ModelStruct {
relationship.AssociationForeignDBName = ToDBName(associationForeignKey) relationship.AssociationForeignDBName = ToDBName(associationForeignKey)
joinTableHandler := JoinTableHandler{} joinTableHandler := JoinTableHandler{}
joinTableHandler.Setup(relationship, many2many, scopeType, elemType) joinTableHandler.Setup(scope.db, relationship, many2many, scopeType, elemType)
relationship.JoinTableHandler = &joinTableHandler relationship.JoinTableHandler = &joinTableHandler
field.Relationship = relationship field.Relationship = relationship
} else { } else {