Fix embedded struct with default value, close #3451

This commit is contained in:
Jinzhu 2020-09-11 10:49:31 +08:00
parent 70a7bd52ca
commit b8a74a80d7
3 changed files with 16 additions and 13 deletions

View File

@ -345,6 +345,7 @@ func (schema *Schema) ParseField(fieldStruct reflect.StructField) *Field {
ef.DBName = prefix + ef.DBName ef.DBName = prefix + ef.DBName
} }
if ef.PrimaryKey {
if val, ok := ef.TagSettings["PRIMARYKEY"]; ok && utils.CheckTruth(val) { if val, ok := ef.TagSettings["PRIMARYKEY"]; ok && utils.CheckTruth(val) {
ef.PrimaryKey = true ef.PrimaryKey = true
} else if val, ok := ef.TagSettings["PRIMARY_KEY"]; ok && utils.CheckTruth(val) { } else if val, ok := ef.TagSettings["PRIMARY_KEY"]; ok && utils.CheckTruth(val) {
@ -360,6 +361,7 @@ func (schema *Schema) ParseField(fieldStruct reflect.StructField) *Field {
ef.HasDefaultValue = false ef.HasDefaultValue = false
} }
} }
}
for k, v := range field.TagSettings { for k, v := range field.TagSettings {
ef.TagSettings[k] = v ef.TagSettings[k] = v

View File

@ -9,8 +9,8 @@ require (
gorm.io/driver/mysql v1.0.1 gorm.io/driver/mysql v1.0.1
gorm.io/driver/postgres v1.0.0 gorm.io/driver/postgres v1.0.0
gorm.io/driver/sqlite v1.1.1 gorm.io/driver/sqlite v1.1.1
gorm.io/driver/sqlserver v1.0.3 gorm.io/driver/sqlserver v1.0.4
gorm.io/gorm v1.9.19 gorm.io/gorm v1.20.0
) )
replace gorm.io/gorm => ../ replace gorm.io/gorm => ../

View File

@ -648,6 +648,7 @@ func TestOffset(t *testing.T) {
if (len(users1) != len(users4)) || (len(users1)-len(users2) != 3) || (len(users1)-len(users3) != 5) { if (len(users1) != len(users4)) || (len(users1)-len(users2) != 3) || (len(users1)-len(users3) != 5) {
t.Errorf("Offset should work") 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) 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) { if (len(users1) != len(users4)) || (len(users1)-len(users2) != 3) || (len(users1)-len(users3) != 5) {