1

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?

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
pkaramol
  • 16,451
  • 43
  • 149
  • 324
  • 1
    Does this answer your question? [How can I work with SQL NULL values and JSON in Golang in a good way?](https://stackoverflow.com/questions/33072172/how-can-i-work-with-sql-null-values-and-json-in-golang-in-a-good-way) – Leon Jan 19 '20 at 22:22
  • I stand between overriding the JSON methods and addressing the issue on the db level as suggested by [this](https://github.com/go-sql-driver/mysql/issues/34#issuecomment-158391340) solution – pkaramol Jan 20 '20 at 07:09

0 Answers0