mirror of https://github.com/go-gorm/gorm.git
Should fill data to smaller struct correctly
This commit is contained in:
parent
a11c9f9b1b
commit
fd3ed7ffaf
5
do.go
5
do.go
|
@ -250,7 +250,10 @@ func (s *Do) query(where ...interface{}) {
|
||||||
for _, value := range columns {
|
for _, value := range columns {
|
||||||
field := dest.FieldByName(snakeToUpperCamel(value))
|
field := dest.FieldByName(snakeToUpperCamel(value))
|
||||||
if field.IsValid() {
|
if field.IsValid() {
|
||||||
values = append(values, dest.FieldByName(snakeToUpperCamel(value)).Addr().Interface())
|
values = append(values, field.Addr().Interface())
|
||||||
|
} else {
|
||||||
|
var null interface{}
|
||||||
|
values = append(values, &null)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
s.err(rows.Scan(values...))
|
s.err(rows.Scan(values...))
|
||||||
|
|
20
gorm_test.go
20
gorm_test.go
|
@ -558,6 +558,22 @@ func TestRunCallbacksAndGetErrors(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestFillSmallerStructCorrectly(t *testing.T) {
|
||||||
|
type SimpleUser struct {
|
||||||
|
Name string
|
||||||
|
Id int64
|
||||||
|
UpdatedAt time.Time
|
||||||
|
CreatedAt time.Time
|
||||||
|
}
|
||||||
|
|
||||||
|
var simple_user SimpleUser
|
||||||
|
db.Table("users").Find(&simple_user)
|
||||||
|
|
||||||
|
if simple_user.Id == 0 || simple_user.Name == "" {
|
||||||
|
t.Errorf("Should fill data correctly even some column missing")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestNoUnExpectedHappenWithInvalidSql(t *testing.T) {
|
func TestNoUnExpectedHappenWithInvalidSql(t *testing.T) {
|
||||||
var columns []string
|
var columns []string
|
||||||
if db.Where("sdsd.zaaa = ?", "sd;;;aa").Pluck("aaa", &columns).Error == nil {
|
if db.Where("sdsd.zaaa = ?", "sd;;;aa").Pluck("aaa", &columns).Error == nil {
|
||||||
|
@ -611,10 +627,6 @@ func TestSetTableDirectly(t *testing.T) {
|
||||||
t.Errorf("Should got some errors if set table to an unexisting table")
|
t.Errorf("Should got some errors if set table to an unexisting table")
|
||||||
}
|
}
|
||||||
|
|
||||||
if db.Table("products").Find(&users).Error == nil {
|
|
||||||
t.Errorf("Should got some errors if set table to an unexisting table")
|
|
||||||
}
|
|
||||||
|
|
||||||
db.Exec("drop table deleted_users;")
|
db.Exec("drop table deleted_users;")
|
||||||
if db.Table("deleted_users").CreateTable(&User{}).Error != nil {
|
if db.Table("deleted_users").CreateTable(&User{}).Error != nil {
|
||||||
t.Errorf("Should create deleted_users table")
|
t.Errorf("Should create deleted_users table")
|
||||||
|
|
Loading…
Reference in New Issue