jackc / pgx

PostgreSQL driver and toolkit for Go
MIT License
10.83k stars 845 forks source link

emit_pointer_for_null_types results in left join scan error #2147

Closed Smithx10 closed 1 month ago

Smithx10 commented 1 month ago

The following results in

2024/10/11 18:20:31 can't scan into dest[8]: failed to scan array element 0: cannot scan NULL into *string
const GetUser = `-- name: GetUser :one
SELECT 
  u.uuid, u.name, u.email, u.alias, u.description, u.created_at, u.updated_at, u.deleted_at, COALESCE(array_agg(g.name), '{}')::text[] AS memberof 
FROM 
  iam_user u 
LEFT JOIN 
  iam_group_member gm ON u.uuid = gm.user_uuid 
LEFT JOIN 
  iam_group g ON gm.group_uuid = g.uuid 
WHERE 
  (
    (u.uuid = $1 AND $1 IS NOT NULL)
  OR
    (u.name = $2 AND $2 IS NOT NULL)
  )
GROUP BY 
  u.uuid
LIMIT 1
`

type GetUserParams struct {
    Uuid *uuid.UUID
    Name *string
}

type GetUserRow struct {
    Uuid        uuid.UUID
    Name        string
    Email       string
    Alias       *string
    Description *string
    CreatedAt   pgtype.Timestamptz
    UpdatedAt   pgtype.Timestamptz
    DeletedAt   pgtype.Timestamptz
    Memberof    []string
}

// generate:
// package: iam.v1
// message: target: user
func (q *Queries) GetUser(ctx context.Context, db DBTX, arg GetUserParams) (GetUserRow, error) {
    row := db.QueryRow(ctx, GetUser, arg.Uuid, arg.Name)
    var i GetUserRow
    err := row.Scan(
        &i.Uuid,
        &i.Name,
        &i.Email,
        &i.Alias,
        &i.Description,
        &i.CreatedAt,
        &i.UpdatedAt,
        &i.DeletedAt,
        &i.Memberof,
    )
    return i, err
}
-- generate:
-- package: iam.v1
-- message: target: user
-- name: GetUser :one
SELECT 
  u.*, COALESCE(array_agg(g.name), '{}')::text[] AS memberof 
FROM 
  iam_user u 
LEFT JOIN 
  iam_group_member gm ON u.uuid = gm.user_uuid 
LEFT JOIN 
  iam_group g ON gm.group_uuid = g.uuid 
WHERE 
  (
    (u.uuid = sqlc.narg('uuid') AND sqlc.narg('uuid') IS NOT NULL)
  OR
    (u.name = sqlc.narg('name') AND sqlc.narg('name') IS NOT NULL)
  )
GROUP BY 
  u.uuid
LIMIT 1;
Smithx10 commented 1 month ago

Accidently opened this in pgx instead of sqlc .DOH!