Skip to content

feat(golang): emit models to a different package#4421

Open
kyleconroy wants to merge 2 commits intomainfrom
claude/plan-sqlc-issue-835-Zvtvq
Open

feat(golang): emit models to a different package#4421
kyleconroy wants to merge 2 commits intomainfrom
claude/plan-sqlc-issue-835-Zvtvq

Conversation

@kyleconroy
Copy link
Copy Markdown
Collaborator

Closes #835.

Adds four Go codegen options that let users place table model structs
and enums in a Go package distinct from the queries package, optionally
shared across multiple SQL blocks:

  • output_models_path: directory for the models file
  • output_models_package: Go package name for the models file
  • output_models_import: import path for the models package
  • output_models_emit: when false, skip emitting models.go and only
    reference an externally-emitted models package

When the models package differs from the queries package, query files
import the models package and reference types as model.User instead of
User. Embedded model structs in synthetic Row structs and enum types
in synthetic Params structs are qualified consistently.

End-to-end fixtures cover both shapes: a single block with models in a
sibling directory, and two query blocks that share one models package.

https://claude.ai/code/session_01Nj4LbGbqFBTU1EPKKNHtNn

claude added 2 commits April 30, 2026 00:48
Closes #835.

Adds four Go codegen options that let users place table model structs
and enums in a Go package distinct from the queries package, optionally
shared across multiple SQL blocks:

  - output_models_path: directory for the models file
  - output_models_package: Go package name for the models file
  - output_models_import: import path for the models package
  - output_models_emit: when false, skip emitting models.go and only
    reference an externally-emitted models package

When the models package differs from the queries package, query files
import the models package and reference types as `model.User` instead of
`User`. Embedded model structs in synthetic Row structs and enum types
in synthetic Params structs are qualified consistently.

End-to-end fixtures cover both shapes: a single block with models in a
sibling directory, and two query blocks that share one models package.

https://claude.ai/code/session_01Nj4LbGbqFBTU1EPKKNHtNn
Two refinements on top of the initial #835 work:

1. output_models_package now defaults to filepath.Base(output_models_path)
   when unset, matching how `package` defaults to base(out). When only
   output_models_emit: false is configured (no path), it falls back to
   filepath.Base(output_models_import).

2. Query files always import the models package under the alias `models`
   and reference types as `models.User`, regardless of the actual package
   name. This keeps generated query code readable without users having to
   think about import aliases. The actual `package X` declaration in
   models.go still uses the configured (or defaulted) package name.

https://claude.ai/code/session_01Nj4LbGbqFBTU1EPKKNHtNn
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Export models to a different package

2 participants