go version)?go1.10 windows/amd64
PostgreSQL 10.2 64-bit
Hello! I am developing an API using Iris and Gorm and I'm struggling with FirstOrCreate and Assign.
So, I'm trying to update a column called "disabled" when my table has a matching row. If not, it creates it.
When I send a request with "disabled": false and there are no matches for userId and genderId, it creates it perfectly. Then I try to send a request with "disabled": true with the same userId and genderId. It works perfectly: "disabled" was updated in the database. But when I try to send another request with "disabled": false with the same userId and genderId as the other requests, it just doesn't update! BeforeUpdate or BeforeSave aren't even triggered!
Relevant code:
import (
"github.com/jinzhu/gorm"
"github.com/satori/go.uuid"
"time"
)
/*
* Arthur A. Bergamaschi <[email protected]>
* 10/03/2018 20:35
*/
type UserGenderPreference struct {
Id *uuid.UUID `json:"id" gorm:"not null;primary_key;type:uuid;default:uuid_generate_v4();"`
User *User `json:"user" gorm:"foreignkey:UserId;"`
UserId *uuid.UUID
GenderIdentity *GenderIdentity `json:"genderIdentity" gorm:"foreignkey:GenderIdentityId;"`
GenderIdentityId *uuid.UUID
UpdatedAt time.Time `json:"updatedAt" gorm:"type:timestamp;default:now()"`
Disabled bool `json:"disabled" gorm:"not null;type:boolean;"`
}
func (UserGenderPreference) TableName() string {
return "users_gender_preferences"
}
func (userGenderPreference *UserGenderPreference) BeforeCreate(scope *gorm.Scope) error {
id, _ := uuid.NewV4()
scope.SetColumn("Id", &id)
return nil
}
func (UserGenderPreferenceService) Update(userId uuid.UUID, genderId uuid.UUID, prop utils.Map) (*models.UserGenderPreference, error) {
db, err := database.GetConnection()
if err != nil {
return nil, err
}
var out models.UserGenderPreference
where := models.UserGenderPreference{UserId: &userId, GenderIdentityId: &genderId}
assign := models.UserGenderPreference{Disabled: prop["disabled"].(bool)}
if err := db.Where(where).Assign(assign).FirstOrCreate(&out).Error; err != nil { // not executing update when the row has a "disabled = true" and assign has a "disabled = false". there are no errors too.
fmt.Errorf("%v", err)
return nil, err
}
return &out, nil
}
Any ideias?
I am having the same issue. Trying to update a row with a boolean value that is true with a false value is not updating that column.
I am having the same issue as you, guys. Do you have any temporary solutions?
me too.
The problem is that false is a zero value and we are updating using struct. The doc says:
Update with struct only works with none zero values, or use map[string]interface{}
Curious how we can make Assign work with map[string]interface{}. @jinzhu any ideas?
I'm facing the same issue when using Assign + FirstOrCreate for boolean values. any workarounds or fixes @jinzhu?
me too.
I got a same problem, but my situation is occured when updating int value by 0. I think it is because that 0 is the zero-value for int. For example, I tried updating int column with 1 it worked.
i have same issue with uint8 trying to reset a field back to zero but it did not refelected!
@jinzhu any idea?
Please help!
This bug ruined my day...
I have same issue with not empty jsonb.
From the docs under "Query with where" (http://doc.gorm.io/crud.html#query)
You could consider to use pointer type or scanner/valuer to avoid this.
// Use pointer value
type User struct {
gorm.Model
Name string
Age *int
}
// Use scanner/valuer
type User struct {
gorm.Model
Name string
Age sql.NullInt64
}
This issue will be automatically closed because it is marked as GORM V1 issue, we have released the public testing GORM V2 release and its documents https://v2.gorm.io/docs/ already, the testing release has been used in some production services for a while, and going to release the final version in following weeks, we are still actively collecting feedback before it, please open a new issue for any suggestion or problem, thank you
Also check out https://github.com/go-gorm/gorm/wiki/GORM-V2-Release-Note-Draft for how to use the public testing version and its changelog
Most helpful comment
I am having the same issue. Trying to update a row with a boolean value that is true with a false value is not updating that column.