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,19 +345,21 @@ func (schema *Schema) ParseField(fieldStruct reflect.StructField) *Field {
ef.DBName = prefix + ef.DBName ef.DBName = prefix + ef.DBName
} }
if val, ok := ef.TagSettings["PRIMARYKEY"]; ok && utils.CheckTruth(val) { if ef.PrimaryKey {
ef.PrimaryKey = true if val, ok := ef.TagSettings["PRIMARYKEY"]; ok && utils.CheckTruth(val) {
} else if val, ok := ef.TagSettings["PRIMARY_KEY"]; ok && utils.CheckTruth(val) { ef.PrimaryKey = true
ef.PrimaryKey = true } else if val, ok := ef.TagSettings["PRIMARY_KEY"]; ok && utils.CheckTruth(val) {
} else { ef.PrimaryKey = true
ef.PrimaryKey = false } else {
ef.PrimaryKey = false
if val, ok := ef.TagSettings["AUTOINCREMENT"]; !ok || !utils.CheckTruth(val) { if val, ok := ef.TagSettings["AUTOINCREMENT"]; !ok || !utils.CheckTruth(val) {
ef.AutoIncrement = false ef.AutoIncrement = false
} }
if ef.DefaultValue == "" { if ef.DefaultValue == "" {
ef.HasDefaultValue = false ef.HasDefaultValue = false
}
} }
} }

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) {