From 4786e830d66afc1895e995df1b47f2dd2b348c4e Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Mon, 9 May 2016 22:32:33 +0800 Subject: [PATCH] Fix create join table --- model_struct.go | 10 ++++++++-- scope.go | 2 ++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/model_struct.go b/model_struct.go index c1885eae..eb3762f4 100644 --- a/model_struct.go +++ b/model_struct.go @@ -71,7 +71,7 @@ type StructField struct { } func (structField *StructField) clone() *StructField { - return &StructField{ + clone := &StructField{ DBName: structField.DBName, Name: structField.Name, Names: structField.Names, @@ -81,11 +81,17 @@ func (structField *StructField) clone() *StructField { IsScanner: structField.IsScanner, HasDefaultValue: structField.HasDefaultValue, Tag: structField.Tag, - TagSettings: structField.TagSettings, + TagSettings: map[string]string{}, Struct: structField.Struct, IsForeignKey: structField.IsForeignKey, Relationship: structField.Relationship, } + + for key, value := range structField.TagSettings { + clone.TagSettings[key] = value + } + + return clone } // Relationship described the relationship between models diff --git a/scope.go b/scope.go index 844df85c..0116da08 100644 --- a/scope.go +++ b/scope.go @@ -1027,6 +1027,7 @@ func (scope *Scope) createJoinTable(field *StructField) { foreignKeyStruct := field.clone() foreignKeyStruct.IsPrimaryKey = false foreignKeyStruct.TagSettings["IS_JOINTABLE_FOREIGNKEY"] = "true" + delete(foreignKeyStruct.TagSettings, "AUTO_INCREMENT") sqlTypes = append(sqlTypes, scope.Quote(relationship.ForeignDBNames[idx])+" "+scope.Dialect().DataTypeOf(foreignKeyStruct)) primaryKeys = append(primaryKeys, scope.Quote(relationship.ForeignDBNames[idx])) } @@ -1037,6 +1038,7 @@ func (scope *Scope) createJoinTable(field *StructField) { foreignKeyStruct := field.clone() foreignKeyStruct.IsPrimaryKey = false foreignKeyStruct.TagSettings["IS_JOINTABLE_FOREIGNKEY"] = "true" + delete(foreignKeyStruct.TagSettings, "AUTO_INCREMENT") sqlTypes = append(sqlTypes, scope.Quote(relationship.AssociationForeignDBNames[idx])+" "+scope.Dialect().DataTypeOf(foreignKeyStruct)) primaryKeys = append(primaryKeys, scope.Quote(relationship.AssociationForeignDBNames[idx])) }