Your Question
Hello. I have a db request with "where" param, which accepts an interface value, and table model one of the columns of which could be int or null:
type ArticlesThreads struct {
ID uint `gorm:"primary_key;column:id;type:INT4;"`
Title string `gorm:"column:title;type:VARCHAR;size:255;"`
Parent uint `gorm:"column:parent;type:INT4;"`
````
````
result := []models.ArticlesThreads{}
where := make(map[string]interface{})
DB.Model(models.ArticlesThreads{}).Where(where).Find(&result)
And If I pass multiple values everything works fine until one of the values is equal nil:
//All this variants return correct value:
where["parent"] = 1
where["parent"] = nil
where["parent"] = []interface{}{1, 2}
//But noone of this:
where["parent"] = 0
where["parent"] = []interface{}{1, nil}
where["parent"] = []interface{}{1, 0}
Expected answer
Please tell me how can I list several values in the interface, one of which will be identical to NULL in database?
I tried to change the field type in the model to sql.NullInt, string, omitempty, json, but nothing helped..
Comment From: a631807682
where["age"] = []interface{}{1, sql.NullInt64{}}
DB.Where(where).Find(&users)
SELECT * FROM `users` WHERE `age` IN (1,NULL) AND `users`.`deleted_at` IS NULL