Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Postgres JSON ->> operator generates string not *string #3792

Open
danthegoodman1 opened this issue Jan 17, 2025 · 0 comments
Open

Postgres JSON ->> operator generates string not *string #3792

danthegoodman1 opened this issue Jan 17, 2025 · 0 comments
Labels
📚 postgresql bug Something isn't working

Comments

@danthegoodman1
Copy link

danthegoodman1 commented Jan 17, 2025

Version

1.27.0

What happened?

-- 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.

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

@danthegoodman1 danthegoodman1 added the bug Something isn't working label Jan 17, 2025
@danthegoodman1 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 danthegoodman1 changed the title Fails to bind JSON field to *string (->> operator generates string not *string) Postgres JSON ->> operator generates string not *string Jan 17, 2025
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
📚 postgresql bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant