postgresql – Why null string values do not work on UPDATE?

Exception or error:

I’m using sqlboiler to generated Go models from SQL and I’ve came across the following problem. When specifying ON CONFLICT via sqlboiler code:

dev := models.Device{
    ID:     deviceID,
    Type:   null.StringFrom("device"),
    // Name:        null.StringFrom(""),
    // Alias:       null.StringFrom(""),
}
err = dev.Upsert(context.Background(),
    db,
    true,
    []string{"id"},
    boil.Whitelist("name", "alias", "type"),
    boil.Whitelist("id", "name", "alias", "type"),
)

I get the following query:

INSERT INTO "devices" ("id", "type") VALUES ($1,$2) ON CONFLICT ("id") DO UPDATE SET "type" = EXCLUDED."type","updated" = EXCLUDED."updated"
[60b16bd2890814e5 {device true} ]

and the following error: (X because of minified example)

sql: Scan error on column index X, name \"name\": converting NULL to string is unsupported'

When I initialize Name and Alias with the commented code it all works OK. Why do I need to initialize it? Why can’t zero values work in this scenario?

How to solve:

Leave a Reply

Your email address will not be published. Required fields are marked *