How We Went All In on sqlc/pgx for Postgres and Go
I&#x27;m a big fan of the database first code generator approach to talking to an SQL database, so much so that I wrote pggen[1] (not to be confused with pggen[2], as far as I can tell a sqlc fork, which I just recently learned about).<p>I&#x27;m a really big partisan of this approach, but I think I&#x27;d like to play the devil&#x27;s advocate here and lay out some of the weaknesses of both a database first approach in general and sqlc in particular.<p>All database first approaches struggle with SQL metaprogramming when compared with a query builder library or an ORM. For the most part, this isn&#x27;t an issue. Just writing SQL and using parameters correctly can get you very far, but there are a few times when you really need it. In particular, faceted search and pagination are both most naturally expressed via runtime metaprogramming of the SQL queries that you want to execute.<p>Another drawback is poor support from the database for this kind of approach. I only really know how postgres does here, and I&#x27;m not sure how well other databases expose their queries. When writing one of these tools you have to resort to tricks like creating temporary views in order infer the argument and return types of a query. This is mostly opaque to the user, but results in weird stuff bubbling up to the API like the tool not being able to infer nullability of arguments and return values well and not being able to support stuff like RETURNING in statements. sqlc is pretty brilliant because it works around this by reimplementing the whole parser and type checker for postgres in go, which is awesome, but also a lot of work to maintain and potentially subtlety wrong.<p>A minor drawback is that you have to retrain your users to write `x = ANY($1)` instead of `x IN ?`. Most ORMs and query builders seem to lean on their metaprogramming abilities to auto-convert array arguments in the host language into tuples. This is terrible and makes it really annoying when you want to actually pass an array into a query with an ORM&#x2F;query builder, but it&#x27;s the convention that everyone is used to.<p>There are some other issues that most of these tools seem to get wrong, but are not impossible in principle to deal with for a database first code generator. The biggest one is correct handling of migrations. Most of these tools, sqlc included, spit out the straight line &quot;obvious&quot; go code that most people would write to scan some data out of a db. They make a struct, then pass each of the field into Scan by reference to get filled in. This works great until you have a query like `SELECT * FROM foos WHERE field = $1` and then run `ALTER TABLE foos ADD COLUMN new_field text`. Now the deployed server is broken and you need to redeploy really fast as soon as you&#x27;ve run migrations. opendoor&#x2F;pggen handles this, but I&#x27;m not aware of other database first code generators that do (though I could definitely have missed one).<p>Also the article is missing a few more tools in this space. <a href="https:&#x2F;&#x2F;github.com&#x2F;xo&#x2F;xo" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;xo&#x2F;xo</a>. <a href="https:&#x2F;&#x2F;github.com&#x2F;gnormal&#x2F;gnorm" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;gnormal&#x2F;gnorm</a>.<p>[1]: <a href="https:&#x2F;&#x2F;github.com&#x2F;opendoor&#x2F;pggen" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;opendoor&#x2F;pggen</a>
[2]: <a href="https:&#x2F;&#x2F;github.com&#x2F;jschaf&#x2F;pggen" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;jschaf&#x2F;pggen</a>