mirror of https://github.com/go-gorm/gorm.git
Fix auto select with smaller struct for slices
This commit is contained in:
parent
0790ff6937
commit
a8655f7947
|
@ -64,14 +64,24 @@ func BuildQuerySQL(db *gorm.DB) {
|
||||||
clauseSelect.Columns[idx] = clause.Column{Name: name, Raw: true}
|
clauseSelect.Columns[idx] = clause.Column{Name: name, Raw: true}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if db.Statement.Schema != nil && db.Statement.ReflectValue.IsValid() && db.Statement.ReflectValue.Type() != db.Statement.Schema.ModelType {
|
} else if db.Statement.Schema != nil && db.Statement.ReflectValue.IsValid() {
|
||||||
stmt := gorm.Statement{DB: db}
|
smallerStruct := false
|
||||||
// smaller struct
|
switch db.Statement.ReflectValue.Kind() {
|
||||||
if err := stmt.Parse(db.Statement.Dest); err == nil {
|
case reflect.Struct:
|
||||||
clauseSelect.Columns = make([]clause.Column, len(stmt.Schema.DBNames))
|
smallerStruct = db.Statement.ReflectValue.Type() != db.Statement.Schema.ModelType
|
||||||
|
case reflect.Slice:
|
||||||
|
smallerStruct = db.Statement.ReflectValue.Type().Elem() != db.Statement.Schema.ModelType
|
||||||
|
}
|
||||||
|
|
||||||
for idx, dbName := range stmt.Schema.DBNames {
|
if smallerStruct {
|
||||||
clauseSelect.Columns[idx] = clause.Column{Name: dbName}
|
stmt := gorm.Statement{DB: db}
|
||||||
|
// smaller struct
|
||||||
|
if err := stmt.Parse(db.Statement.Dest); err == nil && stmt.Schema.ModelType != db.Statement.Schema.ModelType {
|
||||||
|
clauseSelect.Columns = make([]clause.Column, len(stmt.Schema.DBNames))
|
||||||
|
|
||||||
|
for idx, dbName := range stmt.Schema.DBNames {
|
||||||
|
clauseSelect.Columns[idx] = clause.Column{Name: dbName}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -177,6 +177,24 @@ func TestFillSmallerStruct(t *testing.T) {
|
||||||
if !regexp.MustCompile("SELECT .*id.*name.*updated_at.*created_at.* FROM .*users").MatchString(result.Statement.SQL.String()) {
|
if !regexp.MustCompile("SELECT .*id.*name.*updated_at.*created_at.* FROM .*users").MatchString(result.Statement.SQL.String()) {
|
||||||
t.Fatalf("SQL should include selected names, but got %v", result.Statement.SQL.String())
|
t.Fatalf("SQL should include selected names, but got %v", result.Statement.SQL.String())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
result = DB.Session(&gorm.Session{DryRun: true}).Model(&User{}).Find(&User{}, user.ID)
|
||||||
|
|
||||||
|
if regexp.MustCompile("SELECT .*name.* FROM .*users").MatchString(result.Statement.SQL.String()) {
|
||||||
|
t.Fatalf("SQL should not include selected names, but got %v", result.Statement.SQL.String())
|
||||||
|
}
|
||||||
|
|
||||||
|
result = DB.Session(&gorm.Session{DryRun: true}).Model(&User{}).Find(&[]User{}, user.ID)
|
||||||
|
|
||||||
|
if regexp.MustCompile("SELECT .*name.* FROM .*users").MatchString(result.Statement.SQL.String()) {
|
||||||
|
t.Fatalf("SQL should not include selected names, but got %v", result.Statement.SQL.String())
|
||||||
|
}
|
||||||
|
|
||||||
|
result = DB.Session(&gorm.Session{DryRun: true}).Model(&User{}).Find(&[]*User{}, user.ID)
|
||||||
|
|
||||||
|
if regexp.MustCompile("SELECT .*name.* FROM .*users").MatchString(result.Statement.SQL.String()) {
|
||||||
|
t.Fatalf("SQL should not include selected names, but got %v", result.Statement.SQL.String())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestNot(t *testing.T) {
|
func TestNot(t *testing.T) {
|
||||||
|
|
Loading…
Reference in New Issue