News for hackers. Stuff that matters.
Re:What ORMs have taught me: just learn SQL (2014) by zzzeek on Sunday December 17, 2017 @11:19PM (#15949381)
> If you're using an RDBMS, bite the bullet and learn SQL.

If this person spent all that time using Hibernate and then SQLAlchemy, and all that time did not know SQL, then their suffering and bad experiences make complete sense. You absolutely need to know SQL if you're going to use an ORM effectively. Good ORMs are there to automate the repetitive tasks of composing largely boilerplate DML statements, facilitating query composition, providing abstraction for database-specific and driver-specific quirks, providing patterns to map object graphs to relational graphs, and marshaling rows between your object model and database rows - that last one is something your application needs to do whether or not you write raw SQL, so you'll end up inventing that part yourself without an ORM (I recommend doing so, on a less critical project, to learn the kinds of issues that present themselves). None of those things should be about "hiding SQL", and you need to learn SQL first before you work with an ORM.

Re:What ORMs have taught me: just learn SQL (2014) by erikpukinskis on Monday December 18, 2017 @12:01AM (#15949554)
Ok, let’s break this down:

> Good ORMs are there to automate the repetitive tasks of composing largely boilerplate DML statements, facilitating query composition, providing abstraction for database-specific and driver-specific quirks

None of that requires an ORM. A simple query builder will suffice and it will be much easier to debug and much less error prone than an ORM.

> providing patterns to map object graphs to relational graphs, and marshaling rows between your object model and database rows - that last one is something your application needs to do whether or not you write raw SQL

So this is the real ORM juice. And ya, without an ORM you have to do this by hand.

So here’s the question: How well can an ORM map to your data model out of the box? In a lot of cases this is where the mess comes from. You need to set up basically a low level AI that can figure out the “right” thing to do as call sites all over your codebase are making arbitrary queries on a huge API surface.

And so the argument against ORMs is that it will take less time and be less error-prone to write bespoke loaders that take rows and build your data model than it will be to configure that AI such that it can do the “right” thing with arbitrary queries.

(If you don’t like the word “AI” here, use “expert system” instead.)

Re:What ORMs have taught me: just learn SQL (2014) by avmich on Monday December 18, 2017 @12:49AM (#15949702)
> A simple query builder will suffice and it will be much easier to debug and much less error prone than an ORM.

What's the difference? To me, an ORM is largely a query builder.

Re:What ORMs have taught me: just learn SQL (2014) by emilsedgh on Monday December 18, 2017 @03:45AM (#15950320)
ORM: You ask for something and you don't care _how_ its fetched.

Query Builder: You build a query, just not in SQL. So you can get around SQL's limitations (like composition)

Re:What ORMs have taught me: just learn SQL (2014) by andybak on Monday December 18, 2017 @04:16AM (#15950431)
> ORM: You ask for something and you don't care _how_ its fetched.

Try this:

> ORM: You ask for something and because you've researched and found a well-written, quality ORM you trust that it will create a sane query.

or possibly:

> ORM: You ask for something and you accept the tradeoffs vs hand-written queries but you're content that it's the correct tradeoff for your use-case.

There's plenty of places for bottlenecks to hide and premature optimization is the root of at least some evils.

Re:What ORMs have taught me: just learn SQL (2014) by jcadam on Monday December 18, 2017 @08:20AM (#15951393)
> ORM: You ask for something and because you've researched and found a well-written, quality ORM you trust that it will create a sane query.

Ha. More like:

ORM: You've just joined a project already using an ORM selected by an 'architect' that no longer works here. Everything is fine until you start testing your system with a database sufficiently populated with real-world data. You and the DBA spend the next next 6 months trying to get the damn ORM to generate performant SQL (you even call Oracle support, which promptly dispatches an "engineer" who tries to sell you on another $500k of crap you don't need that won't really address the problem). You eventually just start writing queries by hand wherever you find a bottleneck caused by the naive ORM, which you could have done at the outset, but no one who actually knew SQL well enough was on the team back then.

Re:What ORMs have taught me: just learn SQL (2014) by paulryanrogers on Monday December 18, 2017 @08:58AM (#15951597)
IME, it should never take more than a few hours to run down why the ORM made such a query. At a minimum most RDBMS's have query logging and can explain queries.

Ironically the last time I had a big ORM performance problem was Hibernate eager loading all joins. Diagnosing and fixing it didn't take more than an hour. (Though we did have a very experienced DBA at the time.) YMMV

Re:What ORMs have taught me: just learn SQL (2014) by emeraldd on Monday December 18, 2017 @08:58AM (#15951599)
You have to known when to break free of the ORM. They are great for graphs of CRUD operations and pretty nasty for much of anything else. You may come to a different conclusion depending on the project and dataset of course.
Re:What ORMs have taught me: just learn SQL (2014) by dorgo on Monday December 18, 2017 @04:09PM (#15955530)
And when you break free of the ORM prey to god that your ORM doesn't have a hidden cache somewhere. If it does then you spent a week tear out your hair until you figger out that the ORM' cache caused all the trouble.
Re:What ORMs have taught me: just learn SQL (2014) by collyw on Tuesday December 19, 2017 @06:00AM (#15959311)
This sounds like a distinction that you personally have made, that the wider community probably won't agree on.
Re:What ORMs have taught me: just learn SQL (2014) by petre on Monday December 18, 2017 @04:06AM (#15950398)
A query builder aids you at constructing queries, while an ORM builds queries for you, runs them and maps the output to objects. It's more sophisticated than a query builder.
Re:What ORMs have taught me: just learn SQL (2014) by zimpenfish on Monday December 18, 2017 @04:54AM (#15950570)
> A query builder aids you at constructing queries

... that you understand and can be sure are sensible.

> while an ORM builds queries for you

... that you have to hope are sensible.

That's one of the biggest flaws of the ORM for me - you have limited visibility of what it's doing to your DB.

Re:What ORMs have taught me: just learn SQL (2014) by lmm on Monday December 18, 2017 @11:08AM (#15952594)
Isn't that the same reasoning as "I don't like using high-level languages because they limit my visibility of what's running on my processor"? Do you write all your code in assembly?
Re:What ORMs have taught me: just learn SQL (2014) by _grep_ on Monday December 18, 2017 @11:40AM (#15952909)
I think the issue is that if you don't understand what a high-level language is doing under the hood, then you will constantly be surprised by side-effects. You shouldn't be using an ORM to substitute for your lack of understanding SQL; you should be using it to automate tasks.
Re:What ORMs have taught me: just learn SQL (2014) by scarface74 on Monday December 18, 2017 @05:57AM (#15950802)
How so? I can log the the sql being generated and look at the logs. Something I should be doing either way.
Re:What ORMs have taught me: just learn SQL (2014) by zimpenfish on Monday December 18, 2017 @07:13AM (#15951103)
If you have to log the generated SQL to understand what's happening, you're already behind the curve.

And then what do you do if the ORM is generating junk? If the answer is "use a querybuilder/handcrafted SQL for that one", what's the point of the ORM in the first place?

Re:What ORMs have taught me: just learn SQL (2014) by mgkimsal on Monday December 18, 2017 @07:41AM (#15951216)
> And then what do you do if the ORM is generating junk? If the answer is "use a querybuilder/handcrafted SQL for that one", what's the point of the ORM in the first place?

The point may be that 98% of the queries are just fine, and you've saved time vs writing by hand, and it may be easier to read/understand for the next people to have to touch the code.

Re:What ORMs have taught me: just learn SQL (2014) by zaphar on Monday December 18, 2017 @09:09AM (#15951661)
"saved time" at the least optional time to save time. ORM's are a maintenance burden. They obfuscate DB performance behind usually an enormous API surface.

Most software work is maintenance work. Optimizing for initial deployment is shortsighted.

Re:What ORMs have taught me: just learn SQL (2014) by scarface74 on Monday December 18, 2017 @09:41AM (#15951887)
What "enormous" API surface? It's not about "optimizing initial deployment". It's about "optimizing continuous deployment" and having "always releasable Software". I've worked for departments with 15 devs all working on the same codebase and we could release and rollback releases (A/B deployments) every week because we treated the database as a dumb data store. We had multiple branches at the same time, etc.

I've also worked at a companies where all of the logic was in ungodly stored procedures and getting anything released took months and we had a whole two weeks "hardening sprint" because we had to coordinate with the "database developers" and of course the entirety of the business logic was In stored procedures.

Most software being maintenance work is even more of a reason to optimize deployment. What good is unreleased software? The most important part of the business is releasing software. Most of my emphasis asan Architect is making sure we can release fast.

Re:What ORMs have taught me: just learn SQL (2014) by mgkimsal on Monday December 18, 2017 @10:47AM (#15952380)
I like the idea of stored procedures. I get some of the value they bring. However, the few times I've been on projects where sprocs where the primary focus of logic/truth/app... it was always a pain.

* The 'developers' weren't allowed to write the sprocs. We were at the biz meetings, but the DB guys were hardly ever there - their meetings were separate for some reason, but because devs were at the meetings, the devs were the ones who also were the face of the project. When the project was behind, we caught it in the neck, even if we were bottlenecked waiting for the DB team to write their logic and expose it to us.

* The DB team was always fewer people, juggling more projects, and other things like system uptime, maintenance, backups, etc.

* The sprocs were never part of version control or part of any source code that we could ever see as part of normal development. They typically weren't subject to any unit testing process.

The answer to all of this is mostly human management, structuring resources differently, coming up with different processes, etc. But any of those things would have changed the power dynamic, which seemed to be the purposes in those environments. Hey, I can write a stored procedure too - let me write them, and if a DB wants to 'review' them - or really, anyone on the team - please review and let's hammer it out and make it better. But roadblocking projects until the DB guys can 'get around' to writing our mission critical procs is just silly.

One other 'weird' division I saw a few places was this "developers can never have access to production systems - that violates XYZ" (a regulation, or some 'law' that was never produced, etc). I asked what the core issue was, and it was "you can't just have developers going on to production and just making changes on live systems - that's ... (against our policy, etc)". This was particularly challenging in a situation where a critical bug only happened on one production system, and we weren't allowed to replicate the database to another system, nor was anyone with any knowledge of the deployed code allowed to get on to the production system to even see if what was deployed was what we'd developed. But... this was still "our problem". Yet... the DBA in this case was "allowed" to get on the system and hand-write new triggers and sprocs to 'fix' our problem, all without documenting/testing his code, nor committing the code to any repo for us to even have visibility in to the data manipulation he was doing to 'fix' the problem we supposedly cause but couldn't investigate.

Again, I know this isn't a problem specifically with stored procedures. When sprocs have been promoted as the primary interface, however, it's usually been a political/power grab more than a technical benefit. And yes, again, I know there are technical advantages in some cases, but usually not enough to outweigh the drawbacks I've encountered.

Re:What ORMs have taught me: just learn SQL (2014) by collyw on Tuesday December 19, 2017 @06:47AM (#15959471)
Your complaints about stored procedures seem to be about the nature of the company where they were used rather than any inherent flaw with them.
Re:What ORMs have taught me: just learn SQL (2014) by mgkimsal on Tuesday December 19, 2017 @11:21AM (#15961248)
I believe I pointed out that I realize it's a human issue more than a technology one. It was easier for some people to get suckered in to the power dynamics being played because "DBA" was already seen as more of black-magic art sort of thing, and those guys were the "real wizards" and so forth, so whatever they say goes. It's not been everywhere I've ever worked where sprocs were used, but it seems to have been at the places where "stored procedures are law".
Re:What ORMs have taught me: just learn SQL (2014) by scarface74 on Wednesday December 20, 2017 @06:52PM (#15975034)
And it's because most developers can do sql but most "database developers" can't code. So of course they are protective over their code.
Re:What ORMs have taught me: just learn SQL (2014) by weberc2 on Monday December 18, 2017 @09:56AM (#15952010)
You haven't saved time compared to using a query builder though; in fact you've lost time due to the hit to readability and debugability.
Re:What ORMs have taught me: just learn SQL (2014) by collyw on Tuesday December 19, 2017 @06:49AM (#15959477)
How do you know this? You have no idea what bugs were encountered or how easy they were to fix.
Re:What ORMs have taught me: just learn SQL (2014) by weberc2 on Tuesday December 19, 2017 @05:39PM (#15965107)
We're conjecturing about hypothetical bugs. No one in this thread knows what bugs were encountered nor how easy they were to fix; they aren't real bugs. They're symbolic of the bugs that we encounter every day, which give us the experience on which we base our conjecture.
Re:What ORMs have taught me: just learn SQL (2014) by scarface74 on Monday December 18, 2017 @07:45AM (#15951238)
How often is the ORM generating junk? To say that the ORM is useless because occasionally it doesn't generate performing code (with EF and Linq more often than not it does generate performant code) could be applied to any high level construct. But I don't see people giving up modern languages to go back to writing everything in Assembly or even C.

Yes I optimize when my automated performance testing/stress testing, tells me I need to and may write handcrafted sql, but I've also handcrafted some classes in C back in the day when my old Windows Mobile app using the C# compact framework wasn't performing.

Re:What ORMs have taught me: just learn SQL (2014) by zimpenfish on Monday December 18, 2017 @08:28AM (#15951432)
> How often is the ORM generating junk?

From what I've seen (couple of home-grown ones, Class::DBI, ActiveRecord), "more often than you want".

(I'm willing to admit they may not be class-beating examples. :)

Re:What ORMs have taught me: just learn SQL (2014) by scarface74 on Monday December 18, 2017 @08:53AM (#15951572)
I meant a good ORM. But then again, my definition of a good ORM is an ORM with a language that treats queries as a first class citizen. EF with Linq doesn't really act feel like a separate framework since Linq and Expressions are built into the language.
Re:What ORMs have taught me: just learn SQL (2014) by zaphar on Monday December 18, 2017 @09:11AM (#15951672)
Those are more of a query framework than an ORM. If that's your bar then most ORMs are going to fail to meet it.
Re:What ORMs have taught me: just learn SQL (2014) by scarface74 on Monday December 18, 2017 @09:49AM (#15951944)
How is it a "query framework"? Linq works with objects and the Linq expression provider treats the Linq as data that translates the objects to Sql at runtime. That is by definition an Object Relational Mapping.

Of course most ORMs do fall short because most languages don't have the powerful concept of "code as data".

Re:What ORMs have taught me: just learn SQL (2014) by FLUX-YOU on Monday December 18, 2017 @08:11AM (#15951348)
>you're already behind the curve.

SQL syntax is extremely verbose, and compounds the more tables are involved in a query. You're not counting the time the ORM has saved you before having to resort to logging SQL.

Re:What ORMs have taught me: just learn SQL (2014) by akvadrako on Monday December 18, 2017 @05:38AM (#15950726)
SqlAlchemy Core vs SqlAlchemy ORM
Re:What ORMs have taught me: just learn SQL (2014) by cosarara97 on Monday December 18, 2017 @07:40AM (#15951213)
And doctrine DBAL vs doctrine ORM
Re:What ORMs have taught me: just learn SQL (2014) by Animats on Monday December 18, 2017 @03:51AM (#15950343)
ORMs are more useful as insert builders. Putting data from an object into the database is something of a boilerplate process. Queries vary with what you want to ask. Most of the time, you don't need all the fields, so filling up some object just because it has slots for everything is a waste of effort. Especially if it means references to multiple tables.
Re:What ORMs have taught me: just learn SQL (2014) by egeozcan on Monday December 18, 2017 @04:39AM (#15950510)
Good ORMs have Partial<T> and lazy loading of complex properties through proxies, which can be overridden with something like .With(x => x.ComplexProperty).

But of course, as the queries get more and more complex, the flexibility of the ORM syntax approaches the flexibility SQL. In the end, there are many situations one would rather just use SQL.

Re:What ORMs have taught me: just learn SQL (2014) by majewsky on Monday December 18, 2017 @04:43AM (#15950527)
I think the best ORMs are those that just leave out the "Relational" part entirely. So... "OM"?

For example, in Go, I use Gorp, which has a Select() function where you pass in the SELECT query string (plus bound values) and the target type, and it loads every result row into an object of that type. So you can have an arbitrarily complex SQL query as long as it starts with `SELECT one_table.* FROM`. That's a marvelous design.

And when you have to do a query that returns results from multiple tables? Guess what, you just use the normal SQL module from the standard library.

Re:What ORMs have taught me: just learn SQL (2014) by scarface74 on Monday December 18, 2017 @05:56AM (#15950797)
Why is it an either or? EF can do that but then you loose the benefits of a type safe language.
Re:What ORMs have taught me: just learn SQL (2014) by oceanswave on Monday December 18, 2017 @07:45AM (#15951236)
If type safety is so great, why isn’t sql statically type checked
Re:What ORMs have taught me: just learn SQL (2014) by majewsky on Monday December 18, 2017 @08:22AM (#15951402)
SQL is statically type-checked, at least in Postgres.

  postgres=# SELECT * FROM domains WHERE id = 'foo';
  ERROR:  invalid input syntax for integer: "foo"
  LINE 1: SELECT * FROM domains WHERE id = 'foo';
                                           ^
Re:What ORMs have taught me: just learn SQL (2014) by cpfohl on Monday December 18, 2017 @08:42AM (#15951510)
Not just PostGres. That's standard SQL. Columns have data types. Tables have schemata. Schemata are very strongly typed. Can't insert a 'full_name' column into a table without such a column.
Re:What ORMs have taught me: just learn SQL (2014) by da_chicken on Monday December 18, 2017 @09:02AM (#15951616)
Unless you're talking about SQLite, SQL RDBMSs are both static and strongly typed. The systems typically do allow some implicit type conversions, but type is critical to how a table works.
Re:What ORMs have taught me: just learn SQL (2014) by lmm on Monday December 18, 2017 @11:05AM (#15952558)
Because SQL predates a lot of modern techniques. A ground-up replacement written today probably would be statically type checked.
Re:What ORMs have taught me: just learn SQL (2014) by setr on Monday December 18, 2017 @02:13PM (#15954434)
What do you guys think the schema is?
Re:What ORMs have taught me: just learn SQL (2014) by lmm on Monday December 18, 2017 @02:54PM (#15954855)
Dynamic
Re:What ORMs have taught me: just learn SQL (2014) by int_19h on Monday December 18, 2017 @12:57AM (#15949733)
Is it a good one, though?
Re:What ORMs have taught me: just learn SQL (2014) by JamesBarney on Monday December 18, 2017 @01:08AM (#15949775)
Probably better than the one I could build by hand, with far better documentation, and greater ease in googling problems :)
Re:What ORMs have taught me: just learn SQL (2014) by failedxyz on Monday December 18, 2017 @01:21AM (#15949826)
while ORMs can do quite a bit of optimization, they're still general query builders and can't construct the optimal queries for your use case. if you don't know SQL, or you don't know what's going on behind the scenes, your ORM could be performing much larger queries than it really needs to, costing performance and time
Re:What ORMs have taught me: just learn SQL (2014) by gjjrfcbugxbhf on Monday December 18, 2017 @02:47AM (#15950133)
Most ORMs have way too write raw SQL. Furthermore someone using an ORM well will understand how their queries are mapping to SQL.

Your scenario happens with people that either don't know or don't care. They will write crappy queries with any tool.

Re:What ORMs have taught me: just learn SQL (2014) by collyw on Tuesday December 19, 2017 @06:53AM (#15959497)
Sounds about right.

I built a moderately complex application in Django at a previous workplace, using the ORM for most things, until the queries were too complex for the ORM.

Another guy connected to the same database and built some graphs using PHP and SQL. Guess who had to help him write the SQL when the queries got too complex for him? The ORM user.

Re:What ORMs have taught me: just learn SQL (2014) by stouset on Monday December 18, 2017 @02:43AM (#15950117)
But that's a completely orthogonal problem — the point made (way) above is that you need to understand SQL whether or not you use an ORM.
Re:What ORMs have taught me: just learn SQL (2014) by jbergens on Monday December 18, 2017 @02:08AM (#15949988)
Bascially all in-house built query builders I've seen has been much worse.
Re:What ORMs have taught me: just learn SQL (2014) by voodootrucker on Monday December 18, 2017 @01:43AM (#15949912)
It depends on how willing you are to allow your object graph to match your relational schema. The key is to let SQL be SQL. I wrote one that allows you to load data using standard SQL resource files, but it handles the persistence automatically:

https://github.com/bgard6977/sqorm

Flyway & jOOq take a similar approach:

https://www.jooq.org/

Taking the SQL-first approach also allows you to serialize without circular reference problems, since you don't just load the data, you also define a path to decompose the graph into a DAG.

Re:What ORMs have taught me: just learn SQL (2014) by keredson on Monday December 18, 2017 @10:43AM (#15952338)
Re:What ORMs have taught me: just learn SQL (2014) by pweissbrod on Monday December 18, 2017 @08:01AM (#15951303)
My experiences have led me to the standpoint that most ORMs handle three major things:

1. provide idiomatic domain-object oriented query interface which it in turn translates to SQL

2. provide CRUD sql generation

3. provide some sort of session-based object lifecycle change tracking and management

#1 - The generated SQL is important on many levels. As things like HQL/linq/<your QL here> deviates further from the generated sql transparency is lost. SQL is normally brittle compared to your domain language which has better testability and type safety but still you have a handful of queries where it feels more sensible to write the SQL yourself.

#2 - Code which reflects on a type and generates basic insert/select/update/delete is usually pretty naive and easily done. With the exception of complicated legacy databases and iBatis-style tools ORMs which only support #2 arent really worth bothering.

#3 - I've found this to be the real benefit of an ORM. Being able to scope object lifecycles into clear units of work, buffer pending changes until a discrete point and get scoped caches for "free" have been hugely beneficial.

Each ORM unfortunately/inevitably come with great learning curve. It seems to unfortunately/inevitably bring lots of new concepts and conventions to the table required for the user simply must understand. Sometimes it requires you to re-arrange the way you may write your code to be more session-oriented.

I dont like the idea of AI managing how/when to apply changes to storage media. AI can be smart and efficient yes but you lose that important transparency/predictability. On the contrary I prefer very dumb/mechanical/predictable ORM, something not elegant but the behavior of what happens when is well-understood and easily scaled out to a large team. In my experience hibernate, EF, sqlalchemy have that sort of dumb/predictable behavior (however the SQL they sometimes generate can be performant but unreadable).

Re:What ORMs have taught me: just learn SQL (2014) by spdionis on Monday December 18, 2017 @06:31AM (#15950927)
5 years down the road, good luck refactoring your application to not query the same object from the database 3-4 times, and load it only once instead.
Re:What ORMs have taught me: just learn SQL (2014) by oceanswave on Monday December 18, 2017 @07:45AM (#15951233)
You’ve probably refactored the ORM Data layer 3 times in that time period as ORM producers have a hard time figuring out the interface which they wish to provide you
Re:What ORMs have taught me: just learn SQL (2014) by spdionis on Monday December 18, 2017 @08:00AM (#15951294)
Agreed. I'd say my point still stands because duplicated database calls affect performance and scalability and this can grind the project to a halt at a certain point. Also this refactor is much more dangerous/probably buggy.

On the other hand refactoring your data logic is just business as usual, and you will probably do it in both cases anyway.

Re:What ORMs have taught me: just learn SQL (2014) by guru4consulting on Monday December 18, 2017 @12:23PM (#15953318)
caching is a very tricky area with tons of pitfalls. In my opinion, the ORM should not be caching. Let the clients (or any other layer) cache/clear based on their needs.
Re:What ORMs have taught me: just learn SQL (2014) by scarface74 on Monday December 18, 2017 @05:54AM (#15950794)
So the easiest way to avoid using an ORM is to create your own ORM?

What AI do you need? You map your tables to objects and relationships between objects via FK relationships.

Re:What ORMs have taught me: just learn SQL (2014) by pjmlp on Monday December 18, 2017 @06:24AM (#15950898)
I can get very creative with SELECTs, making use of Prolog style queries, which are fully done server side on the database.

Most ORMs will download all the data and evaluate them on the client side, with code that is even more convoluted that the SQL one and thus with less performance.

Re:What ORMs have taught me: just learn SQL (2014) by scarface74 on Monday December 18, 2017 @06:52AM (#15951023)
If your ORM is downloading all of your data and querying client side, then your ORM "is doing it wrong".

Entity Framework and any other Linq to data provider translates the Linq expression to the native language of the source data and does it server side.

Re:What ORMs have taught me: just learn SQL (2014) by pjmlp on Monday December 18, 2017 @07:15AM (#15951111)
I know EF, which is why my comment also mentions "code that is even more convoluted that the SQL one and thus with less performance".

LINQ only allows for a fraction of what is possible with SQL, and good luck having the best queries generated out of it, if the RDMS doesn't happen to be SQL Server.

Re:What ORMs have taught me: just learn SQL (2014) by scarface74 on Monday December 18, 2017 @07:49AM (#15951253)
Actually my DMS is sometimes Mongo and the Mongo driver does pretty well at translating Linq to Mongo Query using the aggregation framework.

It all depends on the talent of the authors of the drivers.

Re:What ORMs have taught me: just learn SQL (2014) by zzzeek on Monday December 18, 2017 @04:12PM (#15955559)
> Most ORMs will download all the data and evaluate them on the client side

I've seen that pattern in lots of homegrown applications, but I've never seen such a thing in a mainstream ORM. Care to provide examples ?

Re:What ORMs have taught me: just learn SQL (2014) by pjmlp on Tuesday December 19, 2017 @02:56AM (#15958518)
I've seen it a lot in entity beans and the early days of hibernate.

Nowadays I only bother with Dapper, MyBatis, using a mix of SQL and stored procedures.

EF only if the RDMS happens to be SQL Server.

Re:What ORMs have taught me: just learn SQL (2014) by sergiosgc on Monday December 18, 2017 @11:04AM (#15952554)
> What AI do you need? You map your tables to objects and relationships between objects via FK relationships.

That is only true in a one-to-many entity relationship (and even so, it is debatable). A one-to-one relationship can be modeled in the two objects, in one of them, or delegated to a third entity. A many-to-many entity relationship can also be handled, in OO, in various different ways. Idem for a ternary relationship or, basically any higher order relation between objects.

This is known, borrowing a term from electrical engineering, as an impedance mismatch between the two models, and it's not an easy problem by any measure.

Re:What ORMs have taught me: just learn SQL (2014) by bpanon on Monday December 18, 2017 @02:56AM (#15950157)
No, no, no, I know more than you.

Here is why.

Bitcoin cryptocurrency AI biomedical supply chain networking quantum systems-thinker.

Re:What ORMs have taught me: just learn SQL (2014) by dang on Tuesday December 19, 2017 @02:42AM (#15958460)
Re:What ORMs have taught me: just learn SQL (2014) by oceanswave on Monday December 18, 2017 @07:47AM (#15951244)
Here, take my money!
Re:What ORMs have taught me: just learn SQL (2014) by kbenson on Monday December 18, 2017 @12:52AM (#15949712)
> so you'll end up inventing that part yourself without an ORM (I recommend doing so, on a less critical project, to learn the kinds of issues that present themselves).

I recommend anyone that has to deal with ORMs create their own at some point, in a non critical project. Not because they will necessarily create the next big thing (but who knows?), but because nothing quite gives you the perspective and appreciation for what these systems can do and why they have their pain points like making one yourself. You'll most likely not use your own module long after you've created it and then again surveyed what's already available, but it's invaluable in making a good assessment of those options as well.

Similarly, making a web framework yields similar benefits.

In both cases, a good understanding of the underlying technologies they build on (SQL and HTTP), is required, and if you don't have it doing in you'll have it coming out the other side (which is really the reason for this in the end).

Similar things exist all along the spectrum. From embedded OS's and compilers to javascript utility libraries.

What it comes down to is that a tool is best utilized when the person knows when and how to apply it appropriately, and that's as often as not an understanding of the tool as it is of the context.

A person intimately familiar with hammers and their uses can build some interesting wood furniture, but they'll likely never achieve the same level of product as a master woodworker that's just well acquainted with a hammer. Investing time and effort into tools provides only so much benefit. At some point, more knowledge of the craft itself is far more beneficial.

Re:What ORMs have taught me: just learn SQL (2014) by andrewstuart on Monday December 18, 2017 @12:24AM (#15949631)
zzzeek - can I take this chance to praise your work on SqlAlchemy. People say there's not enough thanks given to open source developers... here's thanks to you. It's the work of a craftsman.
Re:What ORMs have taught me: just learn SQL (2014) by jgraham on Monday December 18, 2017 @06:39AM (#15950963)
I'm pretty sure that SqlAlchemy has caused more grief and frustration than any other single library. After all if I didn't know about the excellence of SqlAlchemy, maybe I wouldn't get so cross when I have to do anything non-trivial with the Django ORM ;)

Joking aside, SwlAlchemy is very impressive software, and zzzeek deserves all this praise and more. Every time that there's one of these anti-ORM articles I feel like SqlAlchemy pre-emtively addressed all the substantive criticisms in its flexible, well layered, design.

(And in the interests of fairness, Django's ORM is also very good at making simple things simple).

Re:What ORMs have taught me: just learn SQL (2014) by href on Monday December 18, 2017 @02:25AM (#15950055)
+1 SQLAlchemy has had a solution for every problem I threw at it in 5 years of heavy use!
Re:What ORMs have taught me: just learn SQL (2014) by cybergoat on Monday December 18, 2017 @06:32AM (#15950932)
I want to give thanks to all the SqlAlchemy devs!

I am a huge fan of the custom column types -- they have allowed me to have code that works against SQLite as well as a production databases with ease!

Re:What ORMs have taught me: just learn SQL (2014) by nerdwaller on Monday December 18, 2017 @01:49AM (#15949930)
+1 - personally I really like how SQLAlchemy doesn’t abstract away the database so much where all modeling and power is lost. He and the contributors have done an excellent job.
Re:What ORMs have taught me: just learn SQL (2014) by Zancarius on Monday December 18, 2017 @02:28AM (#15950064)
I've always been impressed by zzzeek's near-omnipresent participation. Years ago, I had a question about SQLAlchemy that he'd answered on the mailing list which amazed me given the relative rarity of developer interaction. Yet here we are almost a decade later and he's still answering questions directly, but on many more platforms. I'm actually not convinced he ever sleeps.

I wish more people aspired to be like him, because you know there's no more authoritative answer when you run into a Stack Exchange post and he's offering up his assistance.

Re:What ORMs have taught me: just learn SQL (2014) by flavio81 on Monday December 18, 2017 @05:55AM (#15950795)
+1 SQLAlechemy is an excellent library. Zzzeek, many kind thanks for such a good ORM.
Re:What ORMs have taught me: just learn SQL (2014) by zzzeek on Monday December 18, 2017 @11:25AM (#15952745)
thanks very much and also to all the other great commenters on this thread!
Re:What ORMs have taught me: just learn SQL (2014) by alexandercrohde on Sunday December 17, 2017 @11:40PM (#15949474)
I think there are 3 main reasons ORMs came into common use:

1. As a reaction to common SQL injection from poor libraries not implementing parameterized queries. (2004 or so)

2. Novice engineers not wanting to learn SQL (look I learned how to make a blog in RoR, and I like mongo!)

3. As a theoretical abstraction above the data-store (as though you might someday be able to switch the data-store beneath the ORM)

1 has been solved, 2 was never okay (point of the article), and 3 isn't really okay either because it's too leaky (performance specifics).

Re:What ORMs have taught me: just learn SQL (2014) by JamesBarney on Monday December 18, 2017 @01:15AM (#15949798)
4. The pain in the butt of writing and maintaining your own mapping code.

5. Type checking all your queries.

6. In code query composability.

But I totally agree that ORMs are too leaky of an abstraction for 2 to be really useful, and that 3 is much harder than it appears to be.

Re:What ORMs have taught me: just learn SQL (2014) by pmontra on Monday December 18, 2017 @03:03AM (#15950186)
+1 for these three points.

But I'm really surprised every time people tell me they look at the schema as defined into the ORM instead of at the table in the database.

I'm really jaw dropped the few times I know somebody doesn't even know SQL, only the ORM. Maybe they look at it as if it were the reaction of somebody that thinks you must know assembly if you want to program Ruby, Python or Node (I don't.) Still, if you work with a database you must know it's internal language, SQL or NoSQL. Your going to need it or build a mess.

And involving a DBA early in the project can make your database at least twice as fast, with the right schema and the right queries. Then you translate that into the ORM you want to use.

Re:What ORMs have taught me: just learn SQL (2014) by dizzystar on Monday December 18, 2017 @05:15AM (#15950642)
Woah, who are you interacting with that not knowing SQL is rare?

In my experience, nearly no one knows SQL, and the attitude seems to be that learning it at all is a waste of mental bandwidth.

On the other hand, I wonder if knowing none at all is better than knowing a little.

Re:What ORMs have taught me: just learn SQL (2014) by GFischer on Monday December 18, 2017 @05:51AM (#15950781)
Wow, I've never met a developer that didn't know SQL.

Even 1 year "learn programming fast" courses here in Uruguay have at least the basics. OTOH, Javascript is not taught in many courses so YMMV...

Re:What ORMs have taught me: just learn SQL (2014) by pmontra on Monday December 18, 2017 @05:32AM (#15950706)
I expect that everybody with a degree knows SQL and I'm realizing that I could be wrong. Maybe sometimes I'm the only one in the room that knows it. I'll check it next time I'm at a technical event leaning on the backend side.
Re:What ORMs have taught me: just learn SQL (2014) by tonyedgecombe on Monday December 18, 2017 @09:24AM (#15951763)
I haven't written very much SQL in my career, there is quite a lot of development that doesn't use it.
Re:What ORMs have taught me: just learn SQL (2014) by spdionis on Monday December 18, 2017 @06:37AM (#15950955)
On the other hand learning javascript frameworks... /s
Re:What ORMs have taught me: just learn SQL (2014) by gldalmaso on Monday December 18, 2017 @05:57AM (#15950801)
I find that jOOQ (java) solves all that for me.

Very happy to not have and ORM on our stack for many years.

I believe that the interaction with the database is just about the most important part of code that you need to rely on. We have had messy data written to the database due to misused or misconfigured ORM (perfomance issues, bad orphan handling, session state problems, overflown sequences, to list a few) and decided that we should not rely on all devs touching that code to be experts in the ORM api to avoid the pitfalls, we rather our devs be expert in SQL.

Never had any of them complain about mapping using something like jOOQ. Type checking and composability are also built in.

Re:What ORMs have taught me: just learn SQL (2014) by pjmlp on Monday December 18, 2017 @07:20AM (#15951133)
Yep, MyBatis is also another nice alternative.
Re:What ORMs have taught me: just learn SQL (2014) by pjmlp on Monday December 18, 2017 @01:53AM (#15949943)
DALs can be easily written with libraries like MyBatis, jOOQ, Dapper, ....

Just because we are using SQL doesn't mean we have to do the mapping fully manual.

Re:What ORMs have taught me: just learn SQL (2014) by mattridley on Monday December 18, 2017 @07:52AM (#15951265)
I use jOOQ - I like the benefits of having the static type checking but the ease of expressing with a SQL like DSL.

Plus it plays very nicely with kotlin :)

Re:What ORMs have taught me: just learn SQL (2014) by exclusiv on Sunday December 17, 2017 @11:55PM (#15949526)
I'd add #4 - it's just generally faster and more enjoyable to develop with a good ORM.
Re:What ORMs have taught me: just learn SQL (2014) by randomdata on Monday December 18, 2017 @10:41AM (#15952318)
Although, I think what you plan to do with the data also makes a big difference. Which probably explains why opinions vary so widely.

For short-lived processes, like typical web applications and command line utilities – that load data from the database, do something with it, and then purge it from memory again – I'm becoming less and less convinced that ORMs are actually a benefit.

On the other hand, if your application plans to map the database data to memory for long periods of time, with the need to keep them in sync, then you're probably going to end up writing something that resembles an ORM anyway, and poorly at that. In this case a good ORM is beneficial.

Re:What ORMs have taught me: just learn SQL (2014) by andybak on Monday December 18, 2017 @04:17AM (#15950435)
Yep. Learned SQL. Happy never to have to touch it again.
Re:What ORMs have taught me: just learn SQL (2014) by eru on Sunday December 17, 2017 @11:47PM (#15949503)
For 3: the proper abstraction for relational data is, surprise, a relation.

I used to work in an environment where we had relations as full first class data structures. They are very pleasant to work with.

We actually had 'relation-object-mappers', ie when we had to interact with other systems that didn't use relations, we often mapped them to relations internally to make them play nicer.

Re:What ORMs have taught me: just learn SQL (2014) by yorwba on Monday December 18, 2017 @12:00AM (#15949546)
How were those relations represented? If I understand correctly, you were not just wrapping tables in a database, but using some other backing implementation. What were the most common operations, and what was the performance like?
Re:What ORMs have taught me: just learn SQL (2014) by eru on Monday December 18, 2017 @12:14AM (#15949599)
Oh, the implementation was fairly straight-forward. I think just sorted arrays are something.

It wasn't about speed of execution, but expressiveness when coding. Later on they even added proper type system support.

Common operations were things like map/project, extend, filter, join, collect-by-key / expand, etc.

Just as Codd pointed out in his original papers, relations allow you to not have to make a choice about a hierarchy for your data.

Using key-value store like a hash-table in your program, or the much vaunted has-a relationship between objects would force you to make these choices. Thus making interacting with the data awkward for all but one access pattern.

Relations work best when your program is written in a style that deals largely in immutable data. (What we call 'purely functional', but people in dysfunctional languages have also picked up on the advantages recently.)

Re:What ORMs have taught me: just learn SQL (2014) by int_19h on Monday December 18, 2017 @01:00AM (#15949739)
> Just as Codd pointed out in his original papers, relations allow you to not have to make a choice about a hierarchy for your data.

Or, alternatively, make it really painful when you do actually need to query hierarchies, along the lines of "give me all the tuples above this one in the hierarchy".

Re:What ORMs have taught me: just learn SQL (2014) by eru on Tuesday December 19, 2017 @12:37AM (#15958031)
Datalog can do those kinds of queries--transitive closures--easily, if memory serves right.

Datalog is a specifically chosen subset of Prolog.

Re:What ORMs have taught me: just learn SQL (2014) by orf on Sunday December 17, 2017 @11:56PM (#15949533)
For 3: if you want to distribute a library or service and have no control over the data source.
Re:What ORMs have taught me: just learn SQL (2014) by rickycook on Monday December 18, 2017 @02:31AM (#15950075)
so, i think #3 is less useful for switching out the actual DB server itself in an ongoing project, but i’ve found it immensely useful in a couple of ways:

- replaced the DB driver mid way through a project to a slower, but more complete implementation. this went flawlessly, because it’s all so generic and in the end the same DB under the hood, so a simple change (unless you don’t use an ORM where i could see it being a nightmare)

- started a new project where i had to use MSSQL, which I’d never used before, but i’m a big fan of postgres/sqlalchemy. other than ODBC oddities, it was really simple to write the new app with all the same patterns i was used to with things like update on write, lazy joins, constraint deferral, and i think most importantly would be MIGRATIONS! huge help to have the same migration framework that i was used to

Re:What ORMs have taught me: just learn SQL (2014) by zimpenfish on Monday December 18, 2017 @04:58AM (#15950590)
> As a theoretical abstraction above the data-store

I worked on a project with a home-grown ORM (in C; it was horrible) that abstracted over both MySQL and Postgres ... except the overarching application required Postgres-specific column types and functions.

Re:What ORMs have taught me: just learn SQL (2014) by pvg on Monday December 18, 2017 @12:32AM (#15949651)
ORMs of various stripes were in common use well before 2004 so 1 & 2 don’t sound terribly persuasive. 3 is definitely an evergreen selling point - you might change to a different RDBMS vendor (back when there was such a thing), etc.
Re:What ORMs have taught me: just learn SQL (2014) by cloverich on Monday December 18, 2017 @12:14AM (#15949602)
cause I think its relevant -- note he's the creator of SQLAlchemy.
Re:What ORMs have taught me: just learn SQL (2014) by simula67 on Monday December 18, 2017 @12:18AM (#15949614)
Wikipedia says SQLAlchemy was created by somebody named Michael Bayer : https://en.wikipedia.org/wiki/SQLAlchemy

EDIT: Sorry, my mistake. The partent is talking about the parent comment, not the FTA

Re:What ORMs have taught me: just learn SQL (2014) by SatvikBeri on Monday December 18, 2017 @12:20AM (#15949623)
(Mike Bayer is zzzeek: http://techspot.zzzeek.org/)
Re:What ORMs have taught me: just learn SQL (2014) by xzel on Monday December 18, 2017 @12:26AM (#15949636)
Yeah they're the same person. His HN profile has a link to his blog.
Re:What ORMs have taught me: just learn SQL (2014) by LeonidasXIV on Monday December 18, 2017 @05:08AM (#15950620)
If I link to zzzeek's blog in my HN profile, will I also become Michael Bayer?
Re:What ORMs have taught me: just learn SQL (2014) by manicdee on Sunday December 17, 2017 @11:34PM (#15949452)
This is especially true where the ORM can mask things such as moving a column to an associated table. The ORM knows that object.attribute is now represented in the object_attribute table with the relationship using object_attribute.pk in the object.attribute column (which may or may not be renamed to attribute_id).

No need to rewrite all your SQL, just the ORM description of the model!

Re:What ORMs have taught me: just learn SQL (2014) by Joeri on Monday December 18, 2017 @12:41AM (#15949677)
Realistically though, if you’re querying the same table from that many places your architecture is already in trouble. So editing the queries shouldn’t be that big of a deal.
Re:What ORMs have taught me: just learn SQL (2014) by needusername on Monday December 18, 2017 @01:39AM (#15949900)
> providing abstraction for database-specific and driver-specific quirks

That is quite theoretical. My PRs for fixing non-spec compliant behavior in pgjdbc get rejected because they might break some ORMs (mostly Play). My PRs for adding MariaDB sequence support to Hibernate get rejected because there are additional MariaDB features that Hibernate doesn't support as well.

Re:What ORMs have taught me: just learn SQL (2014) by rickycook on Monday December 18, 2017 @02:22AM (#15950044)
it might be theoretical in hibernate, but sqlalchemy does a TON of this stuff

as an example: https://github.com/zzzeek/sqlalchemy/blob/master/lib/sqlalch...

heaps of this kind of thing are nicely dotted around the code so you don’t have to deal with weird driver quirks, maps “Text” column type to whatever it needs to be in your given database to have an unbounded text blob

id say that’s far more than theoretical

EDIT: typo

EDIT 2: also, on DB specific features, SQLA supports a bunch (not all). eg: https://github.com/zzzeek/sqlalchemy/blob/master/lib/sqlalch...

Re:What ORMs have taught me: just learn SQL (2014) by needusername on Monday December 18, 2017 @03:36PM (#15955253)
In Java land the JDBC driver is supposed to do that. If it doesn't then that's a bug in the driver. If it's a bug in the JDBC driver then the fix needs to go into the JDBC driver, especially if the driver is on GitHub.

The presence of an ORM relying on these bugs should not prevent a bug in the driver from being fixed.

Re:What ORMs have taught me: just learn SQL (2014) by mayankkaizen on Monday December 18, 2017 @12:42AM (#15949683)
>, and you need to learn SQL first before you work with an ORM.

I did the opposite and faced lots of difficulties. Then I concentrated on plain SQL and suddenly I felt like I am understanding ORM much better.

Re:What ORMs have taught me: just learn SQL (2014) by jsight on Monday December 18, 2017 @11:12AM (#15952622)
Exactly. It is amazing how many bad ORMs that I've seen in systems from people who "just used SQL" instead of an ORM.

I've found that the people who know SQL pretty well can actually do good work with or without an ORM. But the maintenance is a lot easier when the abstractions are consistent with the abstractions that are used by a popular ORM.

Re:What ORMs have taught me: just learn SQL (2014) by mathattack on Sunday December 17, 2017 @11:35PM (#15949459)
Everything that isn't SQL tuned to your environment forces you to sacrifice performance at some point. At some point it does an inefficient join. How can you not go back and fix performance issues is you don't know SQL? (It isn't like you need to fix it in C)
Re:What ORMs have taught me: just learn SQL (2014) by X-Istence on Monday December 18, 2017 @01:57AM (#15949952)
SQLAlchemy makes dropping down to SQL where necessary incredibly easy. There where necessary you can tune the queries, but where not necessary you can let the ORM create them for you.

This gives you a lot of flexibility and power, but as zzzeek mentioned, you need to know SQL to understand how what the ORM is inefficient and to be able to replace it as necessary while still letting the ORM do what it is really good at.

Re:What ORMs have taught me: just learn SQL (2014) by stcredzero on Monday December 18, 2017 @03:30PM (#15955182)
you need to learn SQL first before you work with an ORM.

Analogously, I'd say that being able to write a compiler, down to having it generate machine code or assembly, gives you a leg up when using a compiled language. I've met and interviewed a number of coders who had cringeworthy gaps in their knowledge, because to them, a C compiler was just some kind of "magic."