Query with gorm, how to keep the fields in the database with the value of null in struct or null? Struct will become an empty string or 0 by default.

err: = db.Where ("id =?", id). First (& activity) .Error

{"id": 30, "name": null}

the name field of the database is null, but the name in & activity will become "", how to keep it still null

type Activity struct {
    Id      int
    name    string
}
Mar.08,2022

the xorm, I use has the same problem.
this problem cannot be assigned to null,golang using the find method that comes with orm. It does not support string-type nil assignment, so there must be a default value during initialization. The workaround for
is to write the sql sentence, IFNULL (name, "none"), and then use orm to execute the query sql sentence.

sql:= "select id,IFNULL(name,"") from table"
result,err:=db.Query(sql)
The same is true for

updates and additions. The structure body is not assigned. After the insert statement of orm is inserted into the database, it will also be the corresponding default values of 0 and "

.
sql:="update table set ....."
err:=db.Exec(sql)
Menu