forked from mirror/gorm
fix: many2many auto migrate (#5322)
* fix: many2many auto migrate * fix: uuid ossp
This commit is contained in:
parent
19b8d37ae8
commit
373bcf7aca
|
@ -235,7 +235,8 @@ func (schema *Schema) buildMany2ManyRelation(relation *Relationship, field *Fiel
|
|||
Name: joinFieldName,
|
||||
PkgPath: ownField.StructField.PkgPath,
|
||||
Type: ownField.StructField.Type,
|
||||
Tag: removeSettingFromTag(ownField.StructField.Tag, "column", "autoincrement", "index", "unique", "uniqueindex"),
|
||||
Tag: removeSettingFromTag(appendSettingFromTag(ownField.StructField.Tag, "primaryKey"),
|
||||
"column", "autoincrement", "index", "unique", "uniqueindex"),
|
||||
})
|
||||
}
|
||||
|
||||
|
@ -258,7 +259,8 @@ func (schema *Schema) buildMany2ManyRelation(relation *Relationship, field *Fiel
|
|||
Name: joinFieldName,
|
||||
PkgPath: relField.StructField.PkgPath,
|
||||
Type: relField.StructField.Type,
|
||||
Tag: removeSettingFromTag(relField.StructField.Tag, "column", "autoincrement", "index", "unique", "uniqueindex"),
|
||||
Tag: removeSettingFromTag(appendSettingFromTag(relField.StructField.Tag, "primaryKey"),
|
||||
"column", "autoincrement", "index", "unique", "uniqueindex"),
|
||||
})
|
||||
}
|
||||
|
||||
|
|
|
@ -2,6 +2,7 @@ package schema
|
|||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
@ -59,6 +60,14 @@ func removeSettingFromTag(tag reflect.StructTag, names ...string) reflect.Struct
|
|||
return tag
|
||||
}
|
||||
|
||||
func appendSettingFromTag(tag reflect.StructTag, value string) reflect.StructTag {
|
||||
t := tag.Get("gorm")
|
||||
if strings.Contains(t, value) {
|
||||
return tag
|
||||
}
|
||||
return reflect.StructTag(fmt.Sprintf(`gorm:"%s;%s"`, value, t))
|
||||
}
|
||||
|
||||
// GetRelationsValues get relations's values from a reflect value
|
||||
func GetRelationsValues(ctx context.Context, reflectValue reflect.Value, rels []*Relationship) (reflectResults reflect.Value) {
|
||||
for _, rel := range rels {
|
||||
|
|
|
@ -657,3 +657,39 @@ func TestMigrateWithSpecialName(t *testing.T) {
|
|||
AssertEqual(t, true, DB.Migrator().HasTable("coupon_product_1"))
|
||||
AssertEqual(t, true, DB.Migrator().HasTable("coupon_product_2"))
|
||||
}
|
||||
|
||||
// https://github.com/go-gorm/gorm/issues/5320
|
||||
func TestPrimarykeyID(t *testing.T) {
|
||||
if DB.Dialector.Name() != "postgres" {
|
||||
return
|
||||
}
|
||||
|
||||
type MissPKLanguage struct {
|
||||
ID string `gorm:"type:uuid;default:uuid_generate_v4()"`
|
||||
Name string
|
||||
}
|
||||
|
||||
type MissPKUser struct {
|
||||
ID string `gorm:"type:uuid;default:uuid_generate_v4()"`
|
||||
MissPKLanguages []MissPKLanguage `gorm:"many2many:miss_pk_user_languages;"`
|
||||
}
|
||||
|
||||
var err error
|
||||
err = DB.Migrator().DropTable(&MissPKUser{}, &MissPKLanguage{})
|
||||
if err != nil {
|
||||
t.Fatalf("DropTable err:%v", err)
|
||||
}
|
||||
|
||||
DB.Exec(`CREATE EXTENSION IF NOT EXISTS "uuid-ossp";`)
|
||||
|
||||
err = DB.AutoMigrate(&MissPKUser{}, &MissPKLanguage{})
|
||||
if err != nil {
|
||||
t.Fatalf("AutoMigrate err:%v", err)
|
||||
}
|
||||
|
||||
// patch
|
||||
err = DB.AutoMigrate(&MissPKUser{}, &MissPKLanguage{})
|
||||
if err != nil {
|
||||
t.Fatalf("AutoMigrate err:%v", err)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue