Can we build our SQL queries?
Whenever I talk to someone about giving up their beloved ORM, one objection inevitably comes up:
I need my ORM to generate SQL for me.
If I dig deeper, I usually get one of these underlying reasons:
- The desire for SQL-agnosticism. That is, the same code could work with MySQL, PostgreSQL, SQLite, or any other SQL flavor.
- They’re not familiar with all the nuances of SQL queries, joins, etc.
There are both arguably valid concerns.
However, neither of these has anything to do with object-relational mapping, which is what ORMs are for (reminder: ORM stands for Object-Relational Mapping).
If the reason you’re using an ORM is to generate SQL for you, then you don’t need an ORM. You need a SQL query builder! Naturally, most (all?) ORMs are bundled with a query builder, but that doesn’t mean you need all the extra features (bloat?) of an ORM.
Next time you’re facing one of these problems, consider whether you can get by with just a query builder.