db icon indicating copy to clipboard operation
db copied to clipboard

sql: Scan error on column index 1, name "com_name": converting NULL to string is unsupported

Open wzhsh90 opened this issue 3 years ago • 3 comments

type Company struct { Id string db:"id" form:"id" json:"id" ComDesc string db:"com_desc,omitempty" form:"com_desc" json:"com_desc" ComName string db:"com_name" form:"com_name" json:"com_name" }

i use the omitempty tag ,that is not work

wzhsh90 avatar Jul 27 '22 13:07 wzhsh90

Please consider asking questions on Stack Overflow.

Asday avatar Jul 27 '22 14:07 Asday

Or Golang Slack.

VojtechVitek avatar Jul 27 '22 16:07 VojtechVitek

Either make sure the query doesn't return rows with com_desc = null or make use of sql.NullString like so:

type Company struct {
	Id      string         `db:"id" form:"id" json:"id"`
	ComDesc sql.NullString `db:"com_desc,omitempty" form:"com_desc" json:"com_desc"`
	ComName string         `db:"com_name" form:"com_name" json:"com_name"`
}

ChristianGerdes avatar Jul 31 '22 23:07 ChristianGerdes