Live data from Hacker News

What ORMs have taught me: just learn SQL (2014)

woz.posthaven.com

101–110 of 360 posts

Re: What ORMs have taught me: just learn SQL (2014)

#101
post #43

> 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 q…

>, 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)

#102
post #68

Earlier quoted context omitted.

ORMs are bad, because objects aren't particularly useful to deal with most data. And when you've already been through the effort to make your data relational for the database, might as well re-use that effort. That's not to say that SQL is the answer. Proper first class support for relations in your programming language / libraries is great. As for using a DSL, Datalog is worth a look.

> ORMs are bad, because objects aren't particularly useful to deal with most data. Objects are containers of data; that's a crazy statement to make. The relational structure maps pretty cleanly to objects, properties, and collections. However, objects are not good for reporting. And the author mentioned doing 14 joins and hundreds of columns - that smells like a reporting query.

The relational structure maps cleanly to badly designed objects, where most classes have 5-10 fields and most fields are opaque data values with setters and getters. If you wrote classes like that in isolation, you'd have to do some serious work in code review, explaining why a hodgepodge collection of properties is a single unit of responsibility. But I've never worked in an ORM-using system where they weren't endemic.

Re: What ORMs have taught me: just learn SQL (2014)

#104
post #13

Why are we even having this debate? There are some ORMs that bring so much value to the table that you'd be stupid not to use them. Example being Django's ORM or SqlAlchemy. Also be specific in what ORM you are comparing to raw SQL. Are you talking about Hibernate or SQLAlchemy. Are you talking about a query builder? Good ORMs help tremendously with maintainability and security. They also let you drop down to raw SQL…

Django's ORM is actually a good example of where identity is an issue. It lacks composite primary keys. If I have a dependent table that has my real ID, say an order number that is a varchar I have to join to the parent table to do lookups by the order number. I can't doing something like key(order_number, line_number) so I end up hydrating a parent object for operations that only require operations on the dependent objects.

Re: What ORMs have taught me: just learn SQL (2014)

#105
My main complaint with ORMs is that the workflow goes like this:

1. Roughly imagine the query you want to execute

2. Map said query into whatever annoying interface the ORM actually exposes

3. Attempt to reason about its behavior because it probably isn't exactly what you envisioned in step 1

I would much rather just write the damn query in the first place.

Re: What ORMs have taught me: just learn SQL (2014)

#106
post #47

An ORM is not a "query builder" An ORM turns your result into a collection of objects. THAT is what an ORM is for and about. The fact that they are built on top of query builders and lighten the load when developing is just a handy side effect.

To me, it hinges on what you mean by "a collection of objects." If it is just a list C-like structs, that's great. If it involves lazy-loaded child collections, inheritance hierarchies, or any kind of behavior at all, that makes me worried.

The lazy loading is my major annoyance. Makes it very hard to cache the resultant half-populated, stateful objects

Re: What ORMs have taught me: just learn SQL (2014)

#107
post #43

> 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 q…

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…

> 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)

#108

I don't hear people complaining about say LINQ. Maybe it's just that your language and/or ORM or integration thereof suck?

LINQ isn't a ORM. I assume you mean Entity Framework? EF definitely has the foreign key issue. We have around a thousand tables, we tried to generate the classes for all of them including foreign keys, problem is that when you create a context that references even only a single table, it will load everything that is foreign keyed including siblings of siblings of siblings, until you run out of memory. Only way around…

Were you doing serialization or something like that that recursively accessed all of the properties in the model? If so, and if lazy loading was turned on (the unfortunate default) then the serializer would indeed keep exploring the object graph until it either loaded your whole database or hit OOM. That's about the only scenario I can think of that would cause such an issue - EF doesn't eagerly load related entities unless you explicitly tell it to.

Re: What ORMs have taught me: just learn SQL (2014)

#109
post #43

> 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 q…

> 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)

#110

Ah yes - the proverbial "ORMs are bad, just learn SQL" post. This is analogous to saying "don't use frameworks". Sound ridiculous? Yes, yes it is. The law of leaky abstractions applies to many, many things, ORMs included. I would also argue they apply in different degrees, usually related to the design of the ORM (the post mentions SQLAlchemy vs. Hibernate, for example). But consider the following: 1) Why do people s…

> This is analogous to saying "don't use frameworks". Sound ridiculous? Yes, yes it is.

When developing API's in Golang or for microservice / serverless architectures not using a framework might actually make a lot of sense. Also microframeworks (trimmed-down versions compared to opinionated frameworks) are very popular in almost any language.

Post reply on HN