Gorm: Issue with updating fields with empty/default values using Select()

Created on 11 Apr 2018  路  10Comments  路  Source: go-gorm/gorm

When the bool field value is false, gorm doesnt update using .Select(fieldName). Works with Save though.

What version of Go are you using (go version)?

1.10rc2

Which database and its version are you using?

PostgreSQL 10.3

  db.DBManager().Select("BoolField").Model(model).Updates(model)

Model:

package models

type Commodity struct {
  BaseModel
  BoolField bool `gorm:"column:bool_field;type:boolean;default:false" valid:"-" json:"bool_field" form:"bool_field" query:"-"`
}

Most helpful comment

@penguinpowernz No, i stopped using gorm and use query builders + sqlx instead.

All 10 comments

I am not sure whether to solve your problem.
in mysql.

Type Test struct{
  SomeField uint
}

i use the Model(&Test{}).Select("some_field").xxxxx
the gorm will generate the database's column some_field.
confirm your column's real name.and make sure the Model before theSelect.

@dongshimou Can you confirm that it works for you in latest release version?

yes. see the doc

if you use the struct update . make sure the update column is not default value.

for example : v:=Test{}. default value: v.SomeField == 0, if you wan't update to 0,

Model(&Test{}).Update(&v). the some_field column will not be update.

it will be failure.

BoolField bool

The field type is bool. And i can't set it to false because false is a default value for bool.
And bool field cant be set to false with Model(&Test{}).Select("bool_field")

use the map[string]interface{} to update. :)

@dongshimou
I know that its possible to do with map[string]interface{} or .Save().
Ive opened this issue because it should be possible to do with Select() and Update() as well.
https://github.com/jinzhu/gorm/issues/469#issuecomment-95776852

set the field to *bool?

@ik9999 did you find a solution?

@penguinpowernz No, i stopped using gorm and use query builders + sqlx instead.

@ik9999 thanks for replying

Was this page helpful?
0 / 5 - 0 ratings

Related issues

kumarsiva07 picture kumarsiva07  路  3Comments

zeropool picture zeropool  路  3Comments

Quentin-M picture Quentin-M  路  3Comments

bramp picture bramp  路  3Comments

sredxny picture sredxny  路  3Comments