From 2676fa4fb8e3c2b11c6bc72c1fb639c1586f6f3b Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Fri, 31 Jul 2020 18:19:25 +0800 Subject: [PATCH] Remove autoincrement tag for join table, close #3217 --- schema/relationship.go | 4 ++-- schema/utils.go | 2 +- schema/utils_test.go | 1 + tests/postgres_test.go | 4 ++-- 4 files changed, 6 insertions(+), 5 deletions(-) diff --git a/schema/relationship.go b/schema/relationship.go index e67092b4..b7ab4f66 100644 --- a/schema/relationship.go +++ b/schema/relationship.go @@ -220,7 +220,7 @@ func (schema *Schema) buildMany2ManyRelation(relation *Relationship, field *Fiel Name: joinFieldName, PkgPath: ownField.StructField.PkgPath, Type: ownField.StructField.Type, - Tag: removeSettingFromTag(ownField.StructField.Tag, "column"), + Tag: removeSettingFromTag(removeSettingFromTag(ownField.StructField.Tag, "column"), "autoincrement"), }) } @@ -243,7 +243,7 @@ func (schema *Schema) buildMany2ManyRelation(relation *Relationship, field *Fiel Name: joinFieldName, PkgPath: relField.StructField.PkgPath, Type: relField.StructField.Type, - Tag: removeSettingFromTag(relField.StructField.Tag, "column"), + Tag: removeSettingFromTag(removeSettingFromTag(relField.StructField.Tag, "column"), "autoincrement"), }) } diff --git a/schema/utils.go b/schema/utils.go index defa83af..1481d428 100644 --- a/schema/utils.go +++ b/schema/utils.go @@ -50,7 +50,7 @@ func toColumns(val string) (results []string) { } func removeSettingFromTag(tag reflect.StructTag, name string) reflect.StructTag { - return reflect.StructTag(regexp.MustCompile(`(?i)(gorm:.*?)(`+name+`:.*?)(;|("))`).ReplaceAllString(string(tag), "${1}${4}")) + return reflect.StructTag(regexp.MustCompile(`(?i)(gorm:.*?)(`+name+`(:.*?)?)(;|("))`).ReplaceAllString(string(tag), "${1}${5}")) } // GetRelationsValues get relations's values from a reflect value diff --git a/schema/utils_test.go b/schema/utils_test.go index e70169bf..1b47ef25 100644 --- a/schema/utils_test.go +++ b/schema/utils_test.go @@ -13,6 +13,7 @@ func TestRemoveSettingFromTag(t *testing.T) { `gorm:"column:db" other:"before:value;column:db;after:value"`: `gorm:"" other:"before:value;column:db;after:value"`, `gorm:"before:value;column:db ;after:value" other:"before:value;column:db;after:value"`: `gorm:"before:value;after:value" other:"before:value;column:db;after:value"`, `gorm:"before:value;column:db; after:value" other:"before:value;column:db;after:value"`: `gorm:"before:value; after:value" other:"before:value;column:db;after:value"`, + `gorm:"before:value;column; after:value" other:"before:value;column:db;after:value"`: `gorm:"before:value; after:value" other:"before:value;column:db;after:value"`, } for k, v := range tags { diff --git a/tests/postgres_test.go b/tests/postgres_test.go index a0b1fddb..85cd34d4 100644 --- a/tests/postgres_test.go +++ b/tests/postgres_test.go @@ -39,13 +39,13 @@ func TestPostgres(t *testing.T) { } type Post struct { - ID uuid.UUID `gorm:"primary_key;type:uuid;default:uuid_generate_v4()"` + ID uuid.UUID `gorm:"primary_key;type:uuid;default:uuid_generate_v4();autoincrement"` Title string Categories []*Category `gorm:"Many2Many:post_categories"` } type Category struct { - ID uuid.UUID `gorm:"primary_key;type:uuid;default:uuid_generate_v4()"` + ID uuid.UUID `gorm:"primary_key;type:uuid;default:uuid_generate_v4();autoincrement"` Title string Posts []*Post `gorm:"Many2Many:post_categories"` }