sqlc icon indicating copy to clipboard operation
sqlc copied to clipboard

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

Open berk-karaal opened this issue 9 months ago • 5 comments

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.

berk-karaal avatar Mar 06 '25 13:03 berk-karaal

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

nicklasos avatar Jul 10 '25 19:07 nicklasos

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

pgmitche avatar Aug 27 '25 17:08 pgmitche

@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!

nicklasos avatar Aug 31 '25 05:08 nicklasos

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

nikitamarchenko avatar Aug 31 '25 09:08 nikitamarchenko

patiently waiting as well :)

vague2k avatar Nov 10 '25 21:11 vague2k

@kyleconroy any plans to merge this?

thelovekesh avatar Dec 16 '25 20:12 thelovekesh