Should fill data to smaller struct correctly

This commit is contained in:
Jinzhu 2013-10-29 16:11:51 +08:00
parent a11c9f9b1b
commit fd3ed7ffaf
2 changed files with 20 additions and 5 deletions

5
do.go
View File

@ -250,7 +250,10 @@ func (s *Do) query(where ...interface{}) {
for _, value := range columns {
field := dest.FieldByName(snakeToUpperCamel(value))
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...))

View File

@ -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) {
var columns []string
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")
}
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;")
if db.Table("deleted_users").CreateTable(&User{}).Error != nil {
t.Errorf("Should create deleted_users table")