From 2757ac73571c96090acb49dd690d079e2411d785 Mon Sep 17 00:00:00 2001 From: Martin Munilla Date: Wed, 17 Apr 2024 12:25:36 +0200 Subject: [PATCH] fix: restore previous behaviour --- schema/schema.go | 10 ++++++---- schema/schema_test.go | 21 --------------------- tests/primary_key_uuid_test.go | 21 --------------------- tests/upsert_test.go | 2 +- 4 files changed, 7 insertions(+), 47 deletions(-) diff --git a/schema/schema.go b/schema/schema.go index bbc8397d..6d882e86 100644 --- a/schema/schema.go +++ b/schema/schema.go @@ -300,11 +300,13 @@ func ParseWithSpecialTableName(dest interface{}, cacheStore *sync.Map, namer Nam field.AutoIncrement = true } case String: - if !field.HasDefaultValue || field.DefaultValueInterface != nil { - schema.FieldsWithDefaultDBValue = append(schema.FieldsWithDefaultDBValue, field) - } + if _, ok := field.TagSettings["PRIMARYKEY"]; !ok { + if !field.HasDefaultValue || field.DefaultValueInterface != nil { + schema.FieldsWithDefaultDBValue = append(schema.FieldsWithDefaultDBValue, field) + } - field.HasDefaultValue = true + field.HasDefaultValue = true + } } } diff --git a/schema/schema_test.go b/schema/schema_test.go index 9c85ee9e..c7200df0 100644 --- a/schema/schema_test.go +++ b/schema/schema_test.go @@ -341,11 +341,6 @@ func TestStringPrimaryKeyDefault(t *testing.T) { Code string Name string } - type ProductWithNamedPrimaryKey struct { - ProductID string `gorm:"primaryKey"` - Code string - Name string - } product, err := schema.Parse(&Product{}, &sync.Map{}, schema.NamingStrategy{}) if err != nil { @@ -362,20 +357,4 @@ func TestStringPrimaryKeyDefault(t *testing.T) { if !isInDefault { t.Errorf("ID should be fields with default") } - - productWithNamedPrimaryKey, err := schema.Parse(&ProductWithNamedPrimaryKey{}, &sync.Map{}, schema.NamingStrategy{}) - if err != nil { - t.Fatalf("failed to parse product struct with composite primary key, got error %v", err) - } - - isInDefault = false - for _, field := range productWithNamedPrimaryKey.FieldsWithDefaultDBValue { - if field.Name == "ProductID" { - isInDefault = true - break - } - } - if !isInDefault { - t.Errorf("ProductID should be fields with default") - } } diff --git a/tests/primary_key_uuid_test.go b/tests/primary_key_uuid_test.go index 99baddf9..6ade58dc 100644 --- a/tests/primary_key_uuid_test.go +++ b/tests/primary_key_uuid_test.go @@ -14,11 +14,6 @@ func TestStringPrimaryKeyDefault(t *testing.T) { Code string Name string } - type ProductWithNamedPrimaryKey struct { - ProductID uuid.UUID `gorm:"primaryKey"` - Code string - Name string - } product, err := schema.Parse(&Product{}, &sync.Map{}, schema.NamingStrategy{}) if err != nil { @@ -35,20 +30,4 @@ func TestStringPrimaryKeyDefault(t *testing.T) { if !isInDefault { t.Errorf("ID should be fields with default") } - - productWithNamedPrimaryKey, err := schema.Parse(&ProductWithNamedPrimaryKey{}, &sync.Map{}, schema.NamingStrategy{}) - if err != nil { - t.Fatalf("failed to parse product struct with composite primary key, got error %v", err) - } - - isInDefault = false - for _, field := range productWithNamedPrimaryKey.FieldsWithDefaultDBValue { - if field.Name == "ProductID" { - isInDefault = true - break - } - } - if !isInDefault { - t.Errorf("ProductID should be fields with default") - } } diff --git a/tests/upsert_test.go b/tests/upsert_test.go index 047ff58a..eddecd29 100644 --- a/tests/upsert_test.go +++ b/tests/upsert_test.go @@ -62,7 +62,7 @@ func TestUpsert(t *testing.T) { } r := DB.Session(&gorm.Session{DryRun: true}).Clauses(clause.OnConflict{UpdateAll: true}).Create(&RestrictedLanguage{Code: "upsert_code", Name: "upsert_name", Lang: "upsert_lang"}) - if !regexp.MustCompile(`INTO .restricted_languages. .*\(.name.,.lang.,.code.\) .* (SET|UPDATE) .name.=.*.name. RETURNING .code.\W*$`).MatchString(r.Statement.SQL.String()) { + if !regexp.MustCompile(`INTO .restricted_languages. .*\(.code.,.name.,.lang.,\) .* (SET|UPDATE) .name.=.*.name.\W*$`).MatchString(r.Statement.SQL.String()) { t.Errorf("Table with escape character, got %v", r.Statement.SQL.String()) } }