I am declaring a Go model as follows:
sql declaration
CREATE TABLE IF NOT EXISTS "day" (
"id" SERIAL PRIMARY KEY,
"dateday" date NOT NULL,
"nameday" varchar(10) NOT NULL,
"salad" varchar,
"holyday" boolean NOT NULL
);
go
representation
type Day struct {
ID string `db:"id" json:"id"`
Dateday string `db:"dateday" json:"dateday"`
Nameday string `db:"nameday" json:"nameday"`
Salad sql.NullString `db:"salad" json:"salad"`
Holyday bool `db:"holyday" json:"holyday"`
}
I had to use the sql.NullString
type for Salad
field, cause in different case, when Getting results that had NuLL
value for the Salad
field and unmarshaling them, I got the error:
error executing query sql: Scan error on column index 3, name "salad": converting NULL to string is unsupported
The problem now is that I cannot seem to be able to handle incoming POST
requests due to the following error:
json: cannot unmarshal string into Go struct field NewDay.salad of type sql.NullString
How can I address the situation?