You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
-- name: ListJobs :many
-- Returns jobs with reduced data, to reduce payload size
select id,
(data ->> 'PhoneNumber')::text as phone_number,
(data ->> 'ContactName')::text as contact_name,
(data ->> 'State')::text as state
from jobs
In the JSON contact_name output is not always in the JSON, can fails to make a nullable string for the ListJobsRow type.
ContactName in the ListJobsRow will only ever be string or interface{}
COALESCE((data ->> 'ContactName')::text, NULL) as contact_name, makes it an interface{}
CASE WHEN (data ->> 'ContactName') IS NULL THEN NULL ELSE (data ->> 'ContactName')::text END as contact_name, returns a string
Considering the ->> operator can return a NULL if the property doesn't exist in the JSON, it should really be a *string or sql.NullString instead of string.
danthegoodman1
changed the title
Fails to bind JSON field to *string
Fails to bind JSON field to *string (->> operator generates string not *string)
Jan 17, 2025
danthegoodman1
changed the title
Fails to bind JSON field to *string (->> operator generates string not *string)
Postgres JSON ->> operator generates string not *stringJan 17, 2025
Version
1.27.0
What happened?
In the JSON
contact_name
output is not always in the JSON, can fails to make a nullable string for theListJobsRow
type.ContactName
in theListJobsRow
will only ever bestring
orinterface{}
COALESCE((data ->> 'ContactName')::text, NULL) as contact_name,
makes it an interface{}CASE WHEN (data ->> 'ContactName') IS NULL THEN NULL ELSE (data ->> 'ContactName')::text END as contact_name,
returns a stringConsidering the
->>
operator can return aNULL
if the property doesn't exist in the JSON, it should really be a*string
orsql.NullString
instead ofstring
.Relevant log output
Database schema
SQL queries
Configuration
Playground URL
https://play.sqlc.dev/p/e5583c55e36fe049212dd7abbe79d8b560444d58675560b18375ffcb31ef8cd6
What operating system are you using?
macOS
What database engines are you using?
PostgreSQL
What type of code are you generating?
Go
The text was updated successfully, but these errors were encountered: