Gorm: Not updating booleans when using UpdateColumns

Created on 3 Sep 2014  路  2Comments  路  Source: go-gorm/gorm

I've turned on the logger to see the values and I'm noticing that when I update a boolean to false that it doesn't get updated in the database.

When boolean is true

DB.Model(&item).UpdateColumns(Item{
Name: "demo",
Display: true,
})
UPDATE "items" SET "display" = 'true', "name" = 'demo' WHERE ("id" = '1')

When boolean is false

DB.Model(&item).UpdateColumns(Item{
Name: "demo",
Display: false,
})
UPDATE "items" SET "name" = 'demo' WHERE ("id" = '1')

Most helpful comment

gorm will ignore zero-values when update struct. and this is what we can't fix ;( (because golang will always initialize all values, so when update with struct, gorm can't know it is setted by you or initialized by golang)

But you could update the value in a different way:

// solution 1
DB.Model(&page).Update("CommentsEnabled", commentsenabled)

// solution 2
page.Commentsenabled = commentsenabled;
DB.Save(&page)

// solution  3
DB.Model(&page).Updates(map[string]interface{}{"CommentsEnabled": commentsenabled})
///

All 2 comments

gorm will ignore zero-values when update struct. and this is what we can't fix ;( (because golang will always initialize all values, so when update with struct, gorm can't know it is setted by you or initialized by golang)

But you could update the value in a different way:

// solution 1
DB.Model(&page).Update("CommentsEnabled", commentsenabled)

// solution 2
page.Commentsenabled = commentsenabled;
DB.Save(&page)

// solution  3
DB.Model(&page).Updates(map[string]interface{}{"CommentsEnabled": commentsenabled})
///

First i got this error too.
When i update gorm by git yesterday, this bug is fixed
When display is false, it make the sql like this

UPDATE "items" SET "display" = 'false', "name" = 'demo' WHERE ("id" = '1')

However, it works in mysql, the display is set to 0.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Ganitzsh picture Ganitzsh  路  3Comments

kumarsiva07 picture kumarsiva07  路  3Comments

bramp picture bramp  路  3Comments

sredxny picture sredxny  路  3Comments

youtwo123 picture youtwo123  路  3Comments