16 points · by xupybd · 8y ago · 19 comments
That said these exist in droves so they must serve a purpose. I can see the case for database portability but I’ve not worked with a project that needed to shit databases. So are there other cases when an ORM pays off?
2. ORM provide abstraction layer. Otherwise every place needs to know all details of tables it accesses. Migration from db to solr for example is much easier with ORM. Ensuring that you call strip_html or other logic in all relevant places is also easier if you have one entry point you can hook into.
3. Strings compose poorly. Building complex queries from set of parameters sucks.
4. Database portability is crucial, even if you will never migrate. It gives you access to far greater mindshare and toolset.
Is it really that hard though? All we need to do is have a class for Users (or products or whatever) and put all queries in that class.
ORMs are annoying, they add another layer of complexity and take the joy out of just writing a simple query. Now I have to learn yet another syntax which is totally unnatural. There is no guarantee these ORMs convert to optimized queries, and not all ORMs support all SQL features either.
Just reading a 20 line ORM query with multiple complex joins vs the same thing written in plain SQL hurts the eyes and brain.
Yes, and this is very close to doing what orms do.
It starts to hinder once there's a requirement for complex reports. I find, that's where the distatse of ORMs in an organization starts.
The other way to deal with majority of these complex reports is to create a separate OLAP table (model) to write/update too, and pull aggregates from there. But that's treading on a thin line of over-engineering.
ORM's are supposed to be used for Objects. If a report isn't a well-defined object/model, then it's going to fight against an ORM.
I once saw a 1000 line query - which unsurprisingly timed out - generated for getting a single user record from the database! There were several lookups to rate limit failed login attempts etc, however this should be a couple of lines of efficient sql if hand rolled.
I can tolerate something like Rails' ActiveRecord if used with restraint. E.g.
user.addresses << Address.new(blah)
Is very useful, it doesn't get in the way and does all the boring stuff for me. People just need to learn this isn't the only way to access the database, and you can drop to sql if you need to.Fortunately I started developing at a time where there were no ORMs, but I fear there is a new generation of devs who see SQL as some esoteric ASM for databases.
Also, I disagree with database portability as an argument for ORMs. All abstractions are leaky, and trying to unify everything will stop you taking full advantage of your data-store of choice
A well-written ORM generally do not introduce much complexity. For example, C# Linq expressions (yes, I know they are used for many other tasks than db access) aren't more complex than the equivalent sql statements.
That doesn't strike me as a shift, though. Presumably, if sqlite (or, say, its heavier weight, also free, cousin PostgreSQL) can mimic enough functionality of the proprietary company-wide db, why isn't bare SQL enough to access those functions?
Why is SQL not a good enough abstraction? Too difficult to ensure one is writing a portable subset of the language?
> A well-written ORM
This runs into the danger of being a "true Scotsman" type argument if it doesn't apply to at least most popular ORMs (or at least one for each popular language).
> aren't more complex than the equivalent sql statements.
They are, by definition, if one already knows the equivalent SQL statement: one has to learn the additional, separate syntax.
It is, but then developers have to make a conscious effort to stick with a portable subset of SQL. It is much easier to have an ORM do that job for you.
> Too difficult to ensure one is writing a portable subset of the language?
For complex applications, that is very difficult, yes.
> This runs into the danger of being a "true Scotsman" type argument if it doesn't apply to at least most popular ORMs (or at least one for each popular language).
Some of the earlier Java EE ORM:s were truly garbage. They included way to much convenient magic. For example, it was often hard to predict whether a someObj.getFoo() call would trigger a database lookup or not. OP might have cut his teeth on one of those and therefore believe they all introduce extra complexity.
> They are, by definition, if one already knows the equivalent SQL statement: one has to learn the additional, separate syntax.
For example, in C# Linq is already used for functional list processing. Using them for SQL is just a change of backend.
Or perhaps just a validation tool. Since you mentioned testing, it might even be enough to use a stripped-down parser from something like SQLite. Granted, integration could be tough if these don't already exist.
> It is much easier to have an ORM do that job for you.
> For complex applications, that is very difficult, yes.
I was hoping for more than the bare assertion. What makes it so difficult? What makes an application "complex" in this context?
> Some of the earlier Java EE ORM:s were truly garbage.
So modern ones are all (or mostly) well-written?
> For example, in C# Linq is already used for functional list processing. Using them for SQL is just a change of backend.
This sounds very much like an application developer treating the database as merely a dumb datastore, which I suspect (and I admit I'm speculating here) the OP would consider an anti-pattern.
Perhaps that's the real source of the OP's question, the perspective of someone who's well-versed enough with RDBMSes and their ability to manipulate/process data before it even reaches the application, even with only portable[1] SQL.
Hopefully the OP, or someone who responded with a "me too" to the OP can chime in to support or refute my speculation.
[1] I would expect the intersection of MSSQL, Oracle, and Postgres to be suitably powerful
But the best part is if I ever needed to, the ORM isn't stopping me.. like recently I wanted to `insert ignore` thousands of values in the table using a single sql query. Afaik that isn't supported by Eloquent so that I did using query. Rest the ORM is able to handle almost everything and keeps the code much more readable and easier to write.
Also, the argument of it getting in the way is moot too, as you can still call a stored proc via an ORM as well.