From ddd0a11c3ff7bdd0a07d5815bc70485def28f088 Mon Sep 17 00:00:00 2001 From: truongns Date: Thu, 9 Mar 2023 12:49:03 +0900 Subject: [PATCH] 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 --- schema/schema.go | 20 ++++++++++---------- schema/schema_test.go | 15 +++++++++++++++ tests/create_test.go | 3 +-- 3 files changed, 26 insertions(+), 12 deletions(-) diff --git a/schema/schema.go b/schema/schema.go index fab1dcef..17bdb25e 100644 --- a/schema/schema.go +++ b/schema/schema.go @@ -221,16 +221,16 @@ func ParseWithSpecialTableName(dest interface{}, cacheStore *sync.Map, namer Nam } } - if schema.PrioritizedPrimaryField == nil && len(schema.PrimaryFields) == 1 { - schema.PrioritizedPrimaryField = schema.PrimaryFields[0] - } - - // If there are multiple primary keys, the AUTOINCREMENT field is prioritized - if schema.PrioritizedPrimaryField == nil && len(schema.PrimaryFields) > 1 { - for _, field := range schema.PrimaryFields { - if _, ok := field.TagSettings["AUTOINCREMENT"]; ok { - schema.PrioritizedPrimaryField = field - break + if schema.PrioritizedPrimaryField == nil { + 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 + for _, field := range schema.PrimaryFields { + if field.AutoIncrement { + schema.PrioritizedPrimaryField = field + break + } } } } diff --git a/schema/schema_test.go b/schema/schema_test.go index cbe31026..5bc0fb83 100644 --- a/schema/schema_test.go +++ b/schema/schema_test.go @@ -301,6 +301,12 @@ func TestCompositePrimaryKeyWithAutoIncrement(t *testing.T) { Code 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{}) if err != nil { @@ -318,4 +324,13 @@ func TestCompositePrimaryKeyWithAutoIncrement(t *testing.T) { f.Updatable = 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") + } } diff --git a/tests/create_test.go b/tests/create_test.go index 75b26b3f..5df6fcf0 100644 --- a/tests/create_test.go +++ b/tests/create_test.go @@ -556,8 +556,7 @@ func TestCreateWithAutoIncrementCompositeKey(t *testing.T) { 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) }