Remove autoincrement tag for join table, close #3217

This commit is contained in:
Jinzhu 2020-07-31 18:19:25 +08:00
parent dc299b900f
commit 2676fa4fb8
4 changed files with 6 additions and 5 deletions

View File

@ -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"),
})
}

View File

@ -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

View File

@ -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 {

View File

@ -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"`
}