Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

feat(codegen/golang): Allow exporting models to a different package #3874

New issue

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

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

Already on GitHub? Sign in to your account

Open
berk-karaal wants to merge 3 commits into sqlc-dev:main
base: main
Choose a base branch
Loading
from berk-karaal:main

Conversation

Copy link

@berk-karaal berk-karaal commented Mar 6, 2025

Fixes #835

This PR adds exporting models to a different package feature to Go code generator.

I also added a how-to page for this feature, you can check the docs/howto/separate-models-file.md file.

Main code changes made in this PR

  • Added optional output_models_package, models_package_import_path and output_query_files_directory options to gen/go configuration. (internal/codegen/golang/opts/options.go)
  • Added Package field to Struct type to specify the correct type of the Struct since they can be in different package now. (internal/codegen/golang/struct.go and internal/codegen/golang/result.go)
  • Type() method of QueryValue will return {Package}.{Name} for Struct types if the Package field of the Struct is not empty. (internal/codegen/golang/query.go)
  • Added {Package}. prefix to enum types if configuration specifies separate models package. (changed only internal/codegen/golang/postgresql_type.go since only postgresql implementation supports typed enum values)
  • Deciding whether the generated file needs to import the separated models file package. (internal/codegen/golang/imports.go)

Note for sqlc users

You can simply try this feature using my sqlc-gen-go plugin fork. I will try to keep that plugin and this PR in sync. Surely sqlc-gen-go plugin configuration is not same as the sqlc gen configuration but the feature implementation code is the same as this PR.

jlisthood, Esequiel378, dw-0, pdarulewski, Hjaltebb, dinopuguh, skewb1k, Ghibranalj, gvuyk, dimkaufo, and 7 more reacted with hooray emoji sezaakgun, Esequiel378, giuliano-macedo, jlisthood, Ghibranalj, gvuyk, eray-alp, mwdev22, thara, dimkaufo, and 9 more reacted with heart emoji
@dosubot dosubot bot added size:L This PR changes 100-499 lines, ignoring generated files. 🔧 golang labels Mar 6, 2025
dimkaufo added a commit to dimkaufo/sqlc that referenced this pull request May 26, 2025
Copy link

Any plans to merge this PR? It’s a really useful feature for large projects using sqlc.

pdarulewski, TobiasKinexon, AidenHadisi, snickers54, jlisthood, gebes, ignion-damien, pgmitche, nikitamarchenko, berk-karaal, and jamesclare-fleetspace reacted with thumbs up emoji

Copy link

Would love to see some movement on this one, pretty handy feature. What's keeping this from being integrated?

nicklasos, nikitamarchenko, berk-karaal, and jamesclare-fleetspace reacted with thumbs up emoji

Copy link

@kyleconroy hey! I’m working on a big project using SQLC, and it keeps growing. This PR would have a huge positive impact on my project as well as many others. From my perspective, this is the only weak spot in SQLC, and it could be resolved with this PR. I already tested this PR on our codebase, and it works like a charm. Please consider reviewing it when you have a chance, and please let me know if I can help in any way. Thank you!

xomiachuna, nikitamarchenko, berk-karaal, jamesclare-fleetspace, and pdarulewski reacted with thumbs up emoji

Copy link

I need this feature. Can't use without. Please.

nicklasos, berk-karaal, and jamesclare-fleetspace reacted with thumbs up emoji

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Reviewers
No reviews
Assignees
No one assigned
Labels
size:L This PR changes 100-499 lines, ignoring generated files. 🔧 golang
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

Export models to a different package

AltStyle によって変換されたページ (->オリジナル) /