fix: AutoMigrate with special table name (#5301)

* fix: AutoMigrate with special table name

* test: migrate with special table name
This commit is contained in:
Cr 2022-04-27 21:13:48 +08:00 committed by GitHub
parent 6a6dfdae72
commit bd7e42ec65
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 1 deletions

View File

@ -759,7 +759,8 @@ func (m Migrator) ReorderModels(values []interface{}, autoAdd bool) (results []i
Statement: &gorm.Statement{DB: m.DB, Dest: value},
}
beDependedOn := map[*schema.Schema]bool{}
if err := dep.Parse(value); err != nil {
// support for special table name
if err := dep.ParseWithSpecialTableName(value, m.DB.Statement.Table); err != nil {
m.DB.Logger.Error(context.Background(), "failed to parse value %#v, got error %v", value, err)
}
if _, ok := parsedSchemas[dep.Statement.Schema]; ok {

View File

@ -636,3 +636,14 @@ func TestMigrateSerialColumn(t *testing.T) {
AssertEqual(t, v.ID, v.UID)
}
}
// https://github.com/go-gorm/gorm/issues/5300
func TestMigrateWithSpecialName(t *testing.T) {
DB.AutoMigrate(&Coupon{})
DB.Table("coupon_product_1").AutoMigrate(&CouponProduct{})
DB.Table("coupon_product_2").AutoMigrate(&CouponProduct{})
AssertEqual(t, true, DB.Migrator().HasTable("coupons"))
AssertEqual(t, true, DB.Migrator().HasTable("coupon_product_1"))
AssertEqual(t, true, DB.Migrator().HasTable("coupon_product_2"))
}