2 ms·
ORMs solve several problems that depending on what you do may or may not be important for you. Problem #1: translating between objects/classes and table rows i
by jillesvangurp 6y ago
ORMs solve several problems that depending on what you do may or may not be important for you.
Problem #1: translating between objects/classes and table rows is mildly tedious to program depending on what language you use. For small amounts of tables it's actually not that much and it's relatively straightforward to maintain this. I'd argue that having a large number of tables is probably a result of using an ORM and encountering the object impedance mismatch. So, it's a problem you aggravate by using an ORM and not doing a proper domain design. More tables usually means more joins. And that's only convenient because the ORM does this for you. It's also slow and a bit of an anti-pattern whether you use ORMs or not.
Problem #2: writing SQL natively is hard (for some). It can be of course; and you can do some complicated things in SQL. But if your table structure is simple, it's actually fairly easy to write the handful of queries you need. Being able to test and prototype your queries against a development database actually makes it easier. Also, this is another problem that is may be made worse by using an ORM because having too many tables means your queries become a mess of nested joins.
Problem #3: porting queries between different databases is hard as they each have their own SQL dialects and subtle implementation differences. Having your ORM generate the appropriate queries for you side steps this problem. But of course you then lock yourself in to your ORMs way of writing queries making switching to another ORM harder. Also, you get to deal with the limitations of your ORMs support for SQL, which typically is a subset of native features. Also, switching databases is not actually that common as it is quite invasive to do. And of course they can be different enough that even using an in memory database for tests and postgresql for production is going to cause you issues with subtle differences in transaction semantics, or how certain queries work, etc. I usually side step that by just using a dockerized db. That's valid whether you use ORMs or not.
Whether you use ORMs or not, good database design is important and knowing enough SQL that you don't need an ORM is kind of a pre-requisite for that. I've seen a lot of bad ORM usage where the people using it were quite clueless about that and made a lot of rookie mistakes. You can't blame the tools for that obviously.
In my case, the above 3 problems are not really problems I have or that are high on my list of solving. I keep my table structure simple and easy to query. Whenever possible, I dumb my schema down to a glorified document store with just a handful of columns. I can knock those queries out in SQL easily and generally I don't migrate between different databases. In any case, most of my projects also use Elasticsearch so most of my querying is actually done against that instead.