Live data from Hacker News

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

woz.posthaven.com

181–190 of 360 posts

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

#181
Again, ORM haters are missing the point.

It's not about avoiding to write SQL, it's about to have standardized API on which all your architecture can count.

Why do you think Django was so successful ?

Because it was built in a way that allowed a rich and powerful ecosystem to flourish.

The Django ORM is not the best out there and it's doing plenty of silly things. If you don't know SQL and you use it you will be in a world of pain.

However.

Because Django features this ORM it can:

- provide auto-generated forms from db model, outputting HTML and validating user inputs, saving changes automatically to the DB.

- provide auto-generated CRUD views from the db model, that you can extend at will.

- provide auto-generated admin

- provide tookits and helpers to deal with your data: signals, various forms of getters, native object casting, advanced validation, better error messages...

- provide entry points for extending the data manipulation API, in a generic way (fields, managers, etc)

- provide tooling for migrations

- provide auth and permissions

- provide user input cleaning and escaping

- automatically deals with value normalization: encoding, timezones, text/number formats, currencies... There is one entry points for those where you can put custom code, and you don't need custom code most of the time since somebody did the work for you more often than not.

- ensure all django projects look the same, so that it's very easy to move from team to team or train people

- formalize the schema, which became a the documentation and only source of truth for your data, that is commited to your VCS. Wannan know what a Django project is all about ? Check urls.py, settings.py and models.py. Done.

The cherry for this cake is of course the fact 3rd party modules (so called django "apps") can leverage that, which lead to the amazing ecosystem Django has.

- auto-generate REST views from model (eg: django-rest-framework), again that you can tweak as much as you want.

- dozens of auth backends.

- data manipulation (workflow, filtering, dashboard, analytics) that just work.

- tags, search, comments, registration and all those stuff you alway rewrite otherwise.

And because they all use the ORM, they are all compatible with each others. And they all work on Mysql, Oracle, SQlite and Postgres, like the entire rest of the framework, out of the box, for free.

You want to do that in any other framework (except RoR) ? You'll get a lib that do half of it, and let the persistence and API integration work to you. And it will not play with others. And that will be integrated differently on another project. If you have a lib at all ! Oh, and you have to use the proper DB. If you are corporate or startup, it won't be the same one and you better hope the lib author is in your shoes.

All that stuff is easy in Django because you have a centralized, easy to inspect, standard, shareable definition of each of your model in one place.

That's what ORM are for. Not "doh, SQL is hard".

Now you could get some part of those benefits by creating central models using schemas untied to the DB, such as marshmallow. It would be an interesting take, but my guess is that you will end up with interfacing it with your DB with some kind of layer, that would look like an ORM anyway.

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

#182
post #107

Earlier quoted context omitted.

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

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.

Good ORMs have Partial 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)

#183
post #107

Earlier quoted context omitted.

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

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.

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)

#184
post #83

The most cringeworthy thing I heard about ORM's actually happened two weeks ago when I explained our use of a query builder rather than an ORM. The new senior developer was talking about speed (??? uhm… k...) and the benefit of being able to switch between PostgreSQL and... MongoDB. I just cringed up, didn't know what to say. Using the same domain model in an RDMBS as a Document Store? I really didn't know how to res…

ORM stands for Object-Relational Mapping (wikipedia). It is just a way to map domain objects to tables. There is no "promotion the use of state" in that definition. It is your choice to start using state in a (mis)designed manner but please don't blame ORMs for that.

Sorry, I should have been more precise. Not all ORM's are designed the same, far from it.

Many ORMs are built using the Unit of Work / Data Mapping patterns. Such ORM's map your data into a separate domain model and manage this model for you. If your orm has something like an "EntityManager" it has likely implemented this Unit of Work pattern.

A key thing the Unit of Work achieves is to commit changes to the database in a single transaction. You often need to update multiple records in an atomic way within enterprise software.

You might not be faced with such challenges in a simple app, but in monolothic enterprise software it's a core feature of what a good backend server does.

Active Record-based ORMs or query builders aid you only a little in this task; they expose the transaction handling logic so much so that it starts to read as a normal SQL database transaction (and might only be cumbersome to use at worst). Here you, the programmer manages it, similar to a normal SQL transaction.

The Unit-of-Work based ORM is more intelligent. It manages the database transaction for you and figures out any changes that were made to the managed entities. In my experience all Java-built enterprise software (I used Hibernate, EclipseLink and Toplink) are designed this way and make heavy use of it. I've used it with Doctrine in PHP quite a lot, and my guess is C#'s Entity Framework is also built around such concepts. That is a big slice of the ORM market.

Here is where the state comes in; different parts of the applications contribute to creating a single database transaction until flush-time. You as a programmer should know when "flush time" actually happens and understand which entities were marked dirty. That is a lot of hidden state that is managed for you; it is in fact the core of what such ORM's do; managing state until it's ready to be flushed. To make it really advanced, powerful ORMs (the popular enterprisey ones) do a lot of caching too, at different times and at different scopes.

When tackling with such tools the distance to normal SQL becomes very large. I think that is where quite a bit of the hate comes from. It's become very powerful magic.

I've been in places where I had to really understand how this magic works to solve serious performance issues with it. I learned a lot, solving problems that shouldn't have existed in the first place.

I don't like magic. It makes me hide in the corner and cry a little.

My comment was targeted towards the UoW / Data Mapper stuff and much less so to Active Record ORM's.

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

#185
yes yes yes! the point is NOT "learn sql" you already know sql, FINE!

the point is: use sql, not orms!

migrations are best done in pure sql, i'll contend that model-inflation is best done in pure sql also.

another thing: if the format is json, that's already a nested "joined" blob of usable data! it's what the end result of a sql join would achieve, the client often just has to drill into that data blob and everything needed for the entity in question is already there!

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

#186
post #121

Earlier quoted context omitted.

Moving the definition of your sql from your app to the database doesn't impact scalability, as in both cases the database needs to perform the query.

We're talking about stored procedures, which are executable code/business logic.

Yes, and if all your stored procedure is doing is execute a query and return a cursor to the result set, it's using just as much database cpu as with a regular query.

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

#187
> just learn SQL

This is great advice in general for everyone in a role that even slightly touches on ops.

In my day-to-day work, I frequently observe that knowing some SQL (esp. joins, and aggregate functions like SUM/MAX with GROUP BY and HAVING) turns you into some sort of mighty wizard for most people. They're trying to debug a problem in their service and not making progress for hours, and you just walk straight into psql, take a look at the schema, do a few SELECTs, and zoom in on the problem.

Yet nobody seems to consider SQL a valuable skill. I guess it's not buzzwordy enough.

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

#188
post #176
post #107

Earlier quoted context omitted.

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

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.

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

#189
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…

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

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

#190
post #95

Earlier quoted context omitted.

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

Yeah they're the same person. His HN profile has a link to his blog.

If I link to zzzeek's blog in my HN profile, will I also become Michael Bayer?
Post reply on HN