schema.go: use field.AutoIncrement instead of field.TagSettings["AUTOINCREMENT"], add test to check autoincrement:false
create_test.go: remove unused code: drop table CompositeKeyProduct
This commit is contained in:
parent
414f34b00d
commit
ddd0a11c3f
@ -221,16 +221,16 @@ func ParseWithSpecialTableName(dest interface{}, cacheStore *sync.Map, namer Nam
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if schema.PrioritizedPrimaryField == nil && len(schema.PrimaryFields) == 1 {
|
if schema.PrioritizedPrimaryField == nil {
|
||||||
schema.PrioritizedPrimaryField = schema.PrimaryFields[0]
|
if len(schema.PrimaryFields) == 1 {
|
||||||
}
|
schema.PrioritizedPrimaryField = schema.PrimaryFields[0]
|
||||||
|
} else if len(schema.PrimaryFields) > 1 {
|
||||||
// If there are multiple primary keys, the AUTOINCREMENT field is prioritized
|
// If there are multiple primary keys, the AUTOINCREMENT field is prioritized
|
||||||
if schema.PrioritizedPrimaryField == nil && len(schema.PrimaryFields) > 1 {
|
for _, field := range schema.PrimaryFields {
|
||||||
for _, field := range schema.PrimaryFields {
|
if field.AutoIncrement {
|
||||||
if _, ok := field.TagSettings["AUTOINCREMENT"]; ok {
|
schema.PrioritizedPrimaryField = field
|
||||||
schema.PrioritizedPrimaryField = field
|
break
|
||||||
break
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -301,6 +301,12 @@ func TestCompositePrimaryKeyWithAutoIncrement(t *testing.T) {
|
|||||||
Code string
|
Code string
|
||||||
Name string
|
Name string
|
||||||
}
|
}
|
||||||
|
type ProductNonAutoIncrement struct {
|
||||||
|
ProductID uint `gorm:"primaryKey;autoIncrement:false"`
|
||||||
|
LanguageCode uint `gorm:"primaryKey"`
|
||||||
|
Code string
|
||||||
|
Name string
|
||||||
|
}
|
||||||
|
|
||||||
product, err := schema.Parse(&Product{}, &sync.Map{}, schema.NamingStrategy{})
|
product, err := schema.Parse(&Product{}, &sync.Map{}, schema.NamingStrategy{})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -318,4 +324,13 @@ func TestCompositePrimaryKeyWithAutoIncrement(t *testing.T) {
|
|||||||
f.Updatable = true
|
f.Updatable = true
|
||||||
f.Readable = true
|
f.Readable = true
|
||||||
})
|
})
|
||||||
|
|
||||||
|
productNonAutoIncrement, err := schema.Parse(&ProductNonAutoIncrement{}, &sync.Map{}, schema.NamingStrategy{})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to parse productNonAutoIncrement struct with composite primary key, got error %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if productNonAutoIncrement.PrioritizedPrimaryField != nil {
|
||||||
|
t.Fatalf("PrioritizedPrimaryField of non autoincrement composite key should be nil")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -556,8 +556,7 @@ func TestCreateWithAutoIncrementCompositeKey(t *testing.T) {
|
|||||||
Name string
|
Name string
|
||||||
}
|
}
|
||||||
|
|
||||||
err := DB.Migrator().DropTable(&CompositeKeyProduct{})
|
if err := DB.AutoMigrate(&CompositeKeyProduct{}); err != nil {
|
||||||
if err = DB.AutoMigrate(&CompositeKeyProduct{}); err != nil {
|
|
||||||
t.Fatalf("failed to migrate, got error %v", err)
|
t.Fatalf("failed to migrate, got error %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user