Hacker News new | past | comments | ask | show | jobs | submit login

> Why an ORM?

No.

All ORMs are trash. Just learn to use the database instead of some crazy syntax or markup/down.

ORMs hide away important things you are going to want to have control over.

Sure you moved fast at the start, but later you will pay a huge price deORMing your product once you realize a ORM was bad.






I never understood why people are so stubborn about hating on orm.

For example I'm familiar with https://gorm.io and it does save me a lot of time and useless boilerplate.

And guess what, if I ever need to make a complex query, I also happen to know SQL, and I'm just going to make a "raw" query https://gorm.io/docs/sql_builder.html#Raw-SQL and be done with it.

It's not all that hard.

edit:

the other common complaint usually is: "but I don't know what query the orm is going to make..."

Use "Debug" https://gorm.io/docs/session.html#Debug and it will print out exactly what query it's making. Not happy with it? Make a raw query.


> I never understood why people are so stubborn about hating on orm.

Objects are just bad abstractions for representing database interactions. They never map cleanly and any attempt to cover up the incoherence introduces further problems.

Avoiding boilerplate is understandable, but typed schemas and queries exist without hauling in a full ORM.

Of course you can pump out a lot of SQL very quickly with ORMs. There's a lot positive to say about this approach! But you don't tend to end up with code where you can easily tell what's going on.


> Objects are just bad abstractions for representing database interactions.

But they're excellent abstractions for business entities.

> Of course you can pump out a lot of SQL very quickly with ORMs. There's a lot positive to say about this approach! But you don't tend to end up with code where you can easily tell what's going on.

15 years building up massive apps with Django and SQLAlchemy and this has never been a problem.


> 15 years building up massive apps with Django and SQLAlchemy and this has never been a problem.

I guarantee you it is if the reader isn't already intimately familiar with SQLAlchemy.


If your job literally entails using a database abstraction and you can't arse yourself into learning said abstraction then it's a skills issue, just as much as people who want to use ORMs without learning SQL.

I can't speak to SQLAlchemy itself, but ActiveRecord (which I'm sure has both shared and unshared issues) still imposes a high maintenance burden with a small team of highly-skilled developers, both on the performance front and the validating-changes front. I, personally, find it very annoying to read, having to hop around an object hierarchy just to figure out what kind of query is generated and what kind of logic is "silently" added to the query somewhere in said object hierarchy (or, god forbid, mixins/monkey-patching).

You definitely make good points, and all my issues are over-comable with time and effort and knowing which tricks to use, so this truly is a matter of opinion and taste. I'm just pointing out that the idea that this produces more readable code seems far from obvious.


I think it's worth giving SQLAlchemy a try if you're writing a Python app. I've worked with most of the popular ones in Python/JS/Rust, and it's the only one I haven't had to fight with.

I'm back to using bespoke query builders and raw SQL since I don't use Python much anymore, but sometimes miss the syntax and migration system.


IMHO your critiques have more to do with Ruby's promotion of class hierarchies and the fact that ActiveRecord requires inspecting a DB to infer what's going on and Rail's abuse of monkeypatching.

I concur. I’ve used ORMs in certain projects, but iterating and optimizing them later on became untenable. I’ve learned my lesson — all my new projects forgo ORMs by default.

How? ORM logic is as simple or as complicated as the underlying queries and results. In 15 years of building web apps with Python ORMs this has never been a problem across many, many different teams.

A lot of people think SQL is antiquated, and therefore not worth learning. However, SQL is based upon Relation Algebra, which is the foundation of database optimalisation. Any data querying language/library that is not based on relational algebra will lack the performance. In particular ORMs fall under this category. Having said that, there are other languages/libraries out there that do use the relational algebra constructs.

You still need to know SQL when you're using ORM. You also need to know ORM when you're using one. It's really not that hard to make ORM perform well if you spend some time RTFMing.

When was the last time your RDBMS was handling business logic and wasn't just a persistence layer for your application? SQL (the language) isn't composable so you often have a choice of building strings or having many slightly different queries.

Anyway, pros of using an ORM outweight it cons in my opinion.


I respect that some prefer just to use SQL, but that isn't where most stand.

Also, instead of a reactionary "all ORMs are trash," where ORM probably means different things to different people, maybe you could provide some value to the conversation by providing specific points and/or arguments supporting your feelings about ORMs. At the very least, you could provide some citation to an article that does the summarization.


Moving fast is often crucial, but you typically also get more readable code, which is also worth it.

A query/DML that is badly optimized is usually very complex, which means that the ORM's syntax isn't a good fit. No problem, since they typically support raw SQL calls, so nothing is really lost.

You just have to know when to use the right tool for the job, as always.


> Moving fast is often crucial, but you typically also get more readable code, which is also worth it.

I think the exact opposite is true, actually—because of the introduction of stuff like lifecycle hooks it becomes very difficult to figure out how the domain semantics translate to query/execution semantics. Of course some are lighter than that and just e.g. map a table to a domain type (which is much more readable), but that's not typically what catches criticism.


Nonsense. People use ORMs because the vast majority of queries are trivial. If you need something the ORM doesn't provide sufficient control over, only then you move to raw queries for these cases.

It’s has nothing to do with being simple and everything to do with wha the database looks like at the end of the day.

Some ORMs are better than others but if if you have ever looked at a database created by a ORM it always has weird lookup tables, funny names and even with simple objects completely unusable without the ORM.

We live in a multi language world. There is a high chance you are going to want to access this data with a different language. This langue is not going to have the same ORM as such you will have a horrid time access the data.

ORMs often lay their data out in a way that is highly language dependent.

The fact of the matter is SQL is not hard and bypassing the main interface to the database and hand wave it away will always bring you regret when writing any sooty of software other than a toy project.

Your best case is you become an expert in the ORM. Resulting in a skill set that does not transfer easy, language locked and the worst of all bottle necks any changes to the data layer to your ORM expert who at first will be proud and happy and end smug and bitchy as all the data layers request changes will simply be redirected to them.

When people like me say ORMs are trash it’s much more than any of the surface level rebuttals listed here. It’s about the whole life cycle is your project. Adding up all the places ORMs can fuck you just makes it a bad proposition.

God forbid you need to upgrade your database or the ORM version.


> ORMs often lay their data out in a way that is highly language dependent.

Which ORMs did you use? This doesn't sound normal at all. Never saw this with Rails, Ecto, EF, Sybase, or even the legacy project I once worked on that had 4 different ORMs for different parts of the same backend process, using the same database (some were very old and behind slowly phased out over time). Maybe you have ORM confused with CMS (content management system). A CMS can do those things, but that is not an ORM.

> There is a high chance you are going to want to access this data with a different language.

There are tools for that, such as ETL, read replicas, data warehouses, code generators, raw sql, stored procedures, views, just off the top of my head.


Well, different people, different experiences.

Same experience just different time lines.

`if you have ever looked at a database created by a ORM`

You do realize that you can make your own migrations using raw SQL, and still use the ORM with your tables?


Yep, plus learn SQL and you can use it everywhere.

Every language has it's own (multiple) language specific ORMs.


Sorry but anyone who needs to build queries dynamically e.g. for extended/advanced search or ACL is going to significantly benefit from being able to seamlessly mix regular code and query logic. Compare that to the old way of concatenating SQL fragments. Shudder.

And yet, the Vietnam of Computer Science paper didn't stop the creation of ORM's.

There's something fundamentally broken with SQL syntax, and yelling at people to "Just Use SQL" doesn't really help.


Its better than sitting silent and doing nothing. What it does is validate other peoples feelings who feel the same way. And just maybe, that validation will lead to them saying "fuck ORMs" when some junior dev comes in and tries to use one.

Fair, but it also reinforces the stereotype that the pro-SQL types are obstinate relics yelling at the youngsters to "Get off my lawn!"

There's a pattern of "We can't change X, so we'll write Y that transpiles down to X". It happens often with closed source tools and others that can't or won't implement new languages. Verilog, SQL, Javascript, all fit that bill.

E.g. Why is Javascript the only first class language for the browser? For the longest time JS was the only game in town.


> There's something fundamentally broken with SQL syntax

? Are you referring to something specific?


excuse me, what is Vietnam of Computer Science paper? what’s vietnam has to do with it?

I am not anti-ORM, but I do know that reference: https://www.odbms.org/wp-content/uploads/2013/11/031.01-Newa...

Yeah, let's trash the 50 year old industry standard and let's obfuscate the interface to one of the most performance sensitive part of an application. Hell, lets build multiple obfuscators for each language, each with it's own astonishing behavior that turns pathological once you actually get usage on the app.

You know whats also a 50 year old industry Standard? Assembler. Yet, Nobody writes it any more.



Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: