Things I wish I knew about sqlc

A while back, I was able to convince my team to use sqlc. I already used it on some personal projects, but those weren’t complex queries whatsoever. Using sqlc in a commercial setting allowed me to see some of its limitations, or perhaps the shift in the paradigm that you experience while using it. In this article, I want to show you some hacks I found to be useful. Configuring the project First things first, if you have a lot of packages, here’s a cool trick on how to make sure similar config is reused accross different queries: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 --- version: "2" sql: - engine: postgresql queries: internal/account/repository/query.sql schema: &schema db/postgres/migrations gen: go: &go_gen_opts package: repository out: internal/account/repository emit_enum_valid_method: true emit_all_enum_values: true emit_sql_as_comment: true omit_unused_structs: true overrides: # numeric is decimal.Decimal - db_type: pg_catalog.numeric go_type: import: github.com/shopspring/decimal type: Decimal - db_type: pg_catalog.numeric go_type: import: github.com/shopspring/decimal type: Decimal nullable: true # nullable decimal is valid too, need to add it separately - engine: postgresql queries: internal/order/repository/query.sql schema: *schema gen: go: <<: *go_gen_opts out: internal/order/repository overrides: - column: "order.currency" go_type: import: "gitlab.com/letssay/our/custom/moneylib" type: "Currency" ...

February 12, 2025 · 5 min · Zemfira