From b8a74a80d732963df95580eae3316db140a882a4 Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Fri, 11 Sep 2020 10:49:31 +0800 Subject: [PATCH] Fix embedded struct with default value, close #3451 --- schema/field.go | 24 +++++++++++++----------- tests/go.mod | 4 ++-- tests/query_test.go | 1 + 3 files changed, 16 insertions(+), 13 deletions(-) diff --git a/schema/field.go b/schema/field.go index e52a8aef..60dc8095 100644 --- a/schema/field.go +++ b/schema/field.go @@ -345,19 +345,21 @@ func (schema *Schema) ParseField(fieldStruct reflect.StructField) *Field { ef.DBName = prefix + ef.DBName } - if val, ok := ef.TagSettings["PRIMARYKEY"]; ok && utils.CheckTruth(val) { - ef.PrimaryKey = true - } else if val, ok := ef.TagSettings["PRIMARY_KEY"]; ok && utils.CheckTruth(val) { - ef.PrimaryKey = true - } else { - ef.PrimaryKey = false + if ef.PrimaryKey { + if val, ok := ef.TagSettings["PRIMARYKEY"]; ok && utils.CheckTruth(val) { + ef.PrimaryKey = true + } else if val, ok := ef.TagSettings["PRIMARY_KEY"]; ok && utils.CheckTruth(val) { + ef.PrimaryKey = true + } else { + ef.PrimaryKey = false - if val, ok := ef.TagSettings["AUTOINCREMENT"]; !ok || !utils.CheckTruth(val) { - ef.AutoIncrement = false - } + if val, ok := ef.TagSettings["AUTOINCREMENT"]; !ok || !utils.CheckTruth(val) { + ef.AutoIncrement = false + } - if ef.DefaultValue == "" { - ef.HasDefaultValue = false + if ef.DefaultValue == "" { + ef.HasDefaultValue = false + } } } diff --git a/tests/go.mod b/tests/go.mod index 4ddb0b69..f62365f8 100644 --- a/tests/go.mod +++ b/tests/go.mod @@ -9,8 +9,8 @@ require ( gorm.io/driver/mysql v1.0.1 gorm.io/driver/postgres v1.0.0 gorm.io/driver/sqlite v1.1.1 - gorm.io/driver/sqlserver v1.0.3 - gorm.io/gorm v1.9.19 + gorm.io/driver/sqlserver v1.0.4 + gorm.io/gorm v1.20.0 ) replace gorm.io/gorm => ../ diff --git a/tests/query_test.go b/tests/query_test.go index 14150038..36229e2c 100644 --- a/tests/query_test.go +++ b/tests/query_test.go @@ -648,6 +648,7 @@ func TestOffset(t *testing.T) { if (len(users1) != len(users4)) || (len(users1)-len(users2) != 3) || (len(users1)-len(users3) != 5) { t.Errorf("Offset should work") } + DB.Where("name like ?", "OffsetUser%").Order("age desc").Find(&users1).Offset(3).Find(&users2).Offset(5).Find(&users3).Offset(-1).Find(&users4) if (len(users1) != len(users4)) || (len(users1)-len(users2) != 3) || (len(users1)-len(users3) != 5) {