-
Hi, In example of how to use Lines 764 to 797 in eab316e This is the query: WITH user_api_keys AS (
SELECT 1 AS user_id, 101 AS user_api_key_id, 'abc123' AS api_key
), users AS (
SELECT 1 AS user_id, 'John Doe' AS name
)
SELECT
user_api_keys.user_api_key_id,
user_api_keys.user_id,
row(users.*) AS user
FROM user_api_keys
LEFT JOIN users ON users.user_id = user_api_keys.user_id
WHERE user_api_keys.api_key = 'abc123'; But if there is no matching relation (the A solution is to do this: (CASE WHEN users.id IS NOT NULL THEN row(users.*) END) AS user This will return a non-empty Is there a more elegant way? Or can this (not taking empty |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
I do not know of a more elegant way, and I also do not think it is a bug. The problem is that Also, this is not specific to |
Beta Was this translation helpful? Give feedback.
-
@jackc Have you considered adopting something similar to this sqlx pull request? Setting the pointer to users to null instead of scanning the nils into an empty struct.
|
Beta Was this translation helpful? Give feedback.
I do not know of a more elegant way, and I also do not think it is a bug.
The problem is that
row(null, null)
is being scanned intoUser
. But theUserID
andName
fields ofUser
cannot beNULL
.Also, this is not specific to
pgx.RowToStructByNameLax
, the code in question is the general case composite to struct scanning system. You would have the same issue if you calledScan
directly.