fix: AutoMigrate with special table name (#5301)
* fix: AutoMigrate with special table name * test: migrate with special table name
This commit is contained in:
		
							parent
							
								
									6a6dfdae72
								
							
						
					
					
						commit
						bd7e42ec65
					
				| @ -759,7 +759,8 @@ func (m Migrator) ReorderModels(values []interface{}, autoAdd bool) (results []i | |||||||
| 			Statement: &gorm.Statement{DB: m.DB, Dest: value}, | 			Statement: &gorm.Statement{DB: m.DB, Dest: value}, | ||||||
| 		} | 		} | ||||||
| 		beDependedOn := map[*schema.Schema]bool{} | 		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) | 			m.DB.Logger.Error(context.Background(), "failed to parse value %#v, got error %v", value, err) | ||||||
| 		} | 		} | ||||||
| 		if _, ok := parsedSchemas[dep.Statement.Schema]; ok { | 		if _, ok := parsedSchemas[dep.Statement.Schema]; ok { | ||||||
|  | |||||||
| @ -636,3 +636,14 @@ func TestMigrateSerialColumn(t *testing.T) { | |||||||
| 		AssertEqual(t, v.ID, v.UID) | 		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")) | ||||||
|  | } | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 Cr
						Cr