Skip to content

feat(#2348, #2997): add sqlc.nembed() for nullable embeds#4341

Open
luismingati wants to merge 1 commit intosqlc-dev:mainfrom
luismingati:feat/sqlc-nembed-2348-2997
Open

feat(#2348, #2997): add sqlc.nembed() for nullable embeds#4341
luismingati wants to merge 1 commit intosqlc-dev:mainfrom
luismingati:feat/sqlc-nembed-2348-2997

Conversation

@luismingati
Copy link

Summary

  • Adds sqlc.nembed() function for nullable embed support in Go codegen
  • When using LEFT JOINs, sqlc.nembed(table) generates a pointer-typed embed struct (*Struct) that is nil when all joined columns are NULL
  • Scans into temporary pointer variables and conditionally constructs the embedded struct only when at least one field is non-nil
  • Supports both pgx and database/sql (stdlib) drivers, including batch queries
  • Includes end-to-end tests for both drivers

Motivation

Closes #2348
Closes #2997

When using sqlc.embed() with LEFT JOINs, the embedded struct is always populated even when the joined row doesn't exist, resulting in zero-valued fields. sqlc.nembed() (nullable embed) solves this by returning a pointer that is nil when no matching row exists.

Test plan

  • End-to-end tests added for sqlc_nembed with pgx and stdlib drivers
  • Run full test suite: go test --tags=examples -timeout 20m ./...
  • Verify generated code compiles and handles NULL LEFT JOIN rows correctly

🤖 Generated with Claude Code

…beds

Introduces `sqlc.nembed()` function that generates nullable (pointer)
embed structs in Go codegen. When a LEFT JOIN may produce NULL rows,
nembed scans into temporary pointer variables and conditionally
constructs the embedded struct only when at least one field is non-nil.

Closes sqlc-dev#2348
Closes sqlc-dev#2997

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@dosubot dosubot bot added the size:XL This PR changes 500-999 lines, ignoring generated files. label Mar 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:XL This PR changes 500-999 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

sqlc.embed using LEFT JOIN sqlc.embed() generates code that cannot handle NULL items

1 participant