fix: many2many auto migrate
This commit is contained in:
		
							parent
							
								
									19b8d37ae8
								
							
						
					
					
						commit
						20b8cea93f
					
				@ -235,7 +235,8 @@ func (schema *Schema) buildMany2ManyRelation(relation *Relationship, field *Fiel
 | 
				
			|||||||
			Name:    joinFieldName,
 | 
								Name:    joinFieldName,
 | 
				
			||||||
			PkgPath: ownField.StructField.PkgPath,
 | 
								PkgPath: ownField.StructField.PkgPath,
 | 
				
			||||||
			Type:    ownField.StructField.Type,
 | 
								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,
 | 
								Name:    joinFieldName,
 | 
				
			||||||
			PkgPath: relField.StructField.PkgPath,
 | 
								PkgPath: relField.StructField.PkgPath,
 | 
				
			||||||
			Type:    relField.StructField.Type,
 | 
								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 (
 | 
					import (
 | 
				
			||||||
	"context"
 | 
						"context"
 | 
				
			||||||
 | 
						"fmt"
 | 
				
			||||||
	"reflect"
 | 
						"reflect"
 | 
				
			||||||
	"regexp"
 | 
						"regexp"
 | 
				
			||||||
	"strings"
 | 
						"strings"
 | 
				
			||||||
@ -59,6 +60,14 @@ func removeSettingFromTag(tag reflect.StructTag, names ...string) reflect.Struct
 | 
				
			|||||||
	return tag
 | 
						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
 | 
					// GetRelationsValues get relations's values from a reflect value
 | 
				
			||||||
func GetRelationsValues(ctx context.Context, reflectValue reflect.Value, rels []*Relationship) (reflectResults reflect.Value) {
 | 
					func GetRelationsValues(ctx context.Context, reflectValue reflect.Value, rels []*Relationship) (reflectResults reflect.Value) {
 | 
				
			||||||
	for _, rel := range rels {
 | 
						for _, rel := range rels {
 | 
				
			||||||
 | 
				
			|||||||
@ -657,3 +657,37 @@ func TestMigrateWithSpecialName(t *testing.T) {
 | 
				
			|||||||
	AssertEqual(t, true, DB.Migrator().HasTable("coupon_product_1"))
 | 
						AssertEqual(t, true, DB.Migrator().HasTable("coupon_product_1"))
 | 
				
			||||||
	AssertEqual(t, true, DB.Migrator().HasTable("coupon_product_2"))
 | 
						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)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						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…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user