Live data from Hacker News

ORMs are nice but they are the wrong abstraction

workdad.dev

21–30 of 70 posts

Re: ORMs are nice but they are the wrong abstraction

#21
post #13

Every time I see a post talking about how bad ORMs are, they're using ORMs in an awful way. It's not really their fault: most ORMs seem like they encourage the worst ways to use them. But they're entirely missing the point: SQL doesn't have any form of abstraction at all. It's completely impossible to write reusable, adaptable SQL. All SQL must be specifically written bespoke for its individual use. Sure, you can rel…

> SQL doesn't have any form of abstraction at all. It's completely impossible to write reusable, adaptable SQL.

SQL is the abstraction. It's a high-level DSL which saves you from the low-level details of an ORM.

Unfortunately it came out 20 years before the first ORMs, so people default to thinking that ORMS must be an improvement over SQL.

Re: ORMs are nice but they are the wrong abstraction

#22
post #16

The basic problem is that: 1) Relational databases are the best abstraction we've found for storing data. Despite years of attempts (OODB, XML databases, various nosql stores, etc.), we have not been able to improve on it. postgresql, by adding native JSON support, became a better mongo than mongo virtually overnight. 2) Most people never learn databases, and most people who do are idiots working on enterprise three-…

> It should not map onto objects

? To me, this is the main use of an ORM: don't let it do query building for you (unless it's fairly simple LINQ stuff), but just have something map the rows returned to typesafe objects for you.

Re: ORMs are nice but they are the wrong abstraction

#23
post #16

The basic problem is that: 1) Relational databases are the best abstraction we've found for storing data. Despite years of attempts (OODB, XML databases, various nosql stores, etc.), we have not been able to improve on it. postgresql, by adding native JSON support, became a better mongo than mongo virtually overnight. 2) Most people never learn databases, and most people who do are idiots working on enterprise three-…

Can it be database-agnostic and 100% expressive at the same time? Perhaps expressiveness is different depending on the engine. SQLx comes close to this btw.

Yes.

The basic data model was invented a half-century ago. Expressiveness of basic SQL is not different depending on the engine. Relational algebra is the same however it's implemented.

Some databases have extensions, and those are okay to include. You should be able to either (1) choose to not use those or (2) lock yourself into a subset of databases which support the extension you need.

It's good if those extension were namespaced. For example:

* If I want to use a postgres JSON type, it should live under postgres.json (or if it cuts across multiple databases, perhaps extensions.json or similar)

* Likewise, database built-in functions, except for common ones like min/max/mean, should be available under the namespace of those databases.

* It's okay if some of those are abstracted out into a namespace like extensions.math, so I can use extensions.math.sin no matter whether the underlying datastore decided to call it SINE, SIN, SINE_RADIANS, SIN(x/180*PI), and if it doesn't have sine, an exception gets raised.

The basic relational data model provides the best expressiveness for representing data and queries created to date, and doesn't differ. It's a good theoretical model, and it works well in practice. There's good reason it's survived this long, despite no lack of competition. The places expressiveness differs are relatively surface things like data types and functions.

It's also okay to have compound types, where I am explicit about what the type is in different databases. e.g.: string_type = {mysql: 'TEXT', postgresql: ...

Re: ORMs are nice but they are the wrong abstraction

#24
post #22
post #16

The basic problem is that: 1) Relational databases are the best abstraction we've found for storing data. Despite years of attempts (OODB, XML databases, various nosql stores, etc.), we have not been able to improve on it. postgresql, by adding native JSON support, became a better mongo than mongo virtually overnight. 2) Most people never learn databases, and most people who do are idiots working on enterprise three-…

> It should not map onto objects ? To me, this is the main use of an ORM: don't let it do query building for you (unless it's fairly simple LINQ stuff), but just have something map the rows returned to typesafe objects for you.

Mapping rows returned from a query onto typesafe objects is okay, but doesn't require an ORM. It's quite literally just making sure each row:

1) Has the correct native type

2) Can be accessed by name

Many normal database APIs do that themselves. I guess there's a little bit more for writes.

The point of ORMs is that they generally map objects onto tables. You create a class, and the ORM will create the database table for you based on what you have in the class. For example, for Django, you write:

   class Blog(models.Model):
       name = models.CharField(max_length=100)
       tagline = models.TextField()

       def __str__(self):
           return self.name
(source: https://docs.djangoproject.com/en/5.0/topics/db/queries/)

And it will make the table for you. If you change the class, it will make the migration for you. That's clean, simple, and easy if you're not doing anything complex. It saves a ton of work.

However, this is a Very Bad Idea for complex systems, since exactly as you point out, there shouldn't be a 1:1 mapping between tables and classes. There may very well be a 1:1 or many:1 mapping between queries and classes. More sophisticated ORMs can do a bit of that, but at some point, you run into the equivalent of Greenspun's Tenth Rule, where the ORM turns into a more complex, buggy version of SQL.

Re: ORMs are nice but they are the wrong abstraction

#25
post #16

The basic problem is that: 1) Relational databases are the best abstraction we've found for storing data. Despite years of attempts (OODB, XML databases, various nosql stores, etc.), we have not been able to improve on it. postgresql, by adding native JSON support, became a better mongo than mongo virtually overnight. 2) Most people never learn databases, and most people who do are idiots working on enterprise three-…

Sounds like jOOQ?

https://www.jooq.org/

Note that although advertised as for Java, it really supports any language that can run on the JVM which is many of them these days.

Re: ORMs are nice but they are the wrong abstraction

#26
The most interesting/fresh approach I've seen to this problem is Permazen.

https://github.com/permazen/permazen/

It starts by asking what the most natural way is to integrate persistence with a programming language (Java in this case, but the concepts are generic), and then goes ahead and implements the features of an RDBMS as an in-process library that can be given different storage backends as long as they implement a sorted K/V store. So it can sit on top of a simple in-process file based K/V store, RocksDB, FoundationDB, or any SQL database like PostgreSQL, SQLite, Spanner, etc (it just uses the RDBMS to store sorted key/value pairs in that case).

Essentially it's a way to map object graphs to key/value pairs but with the usual features you'd want like indexing, validation, transactions, and so on. The design is really nice and can scale from tiny tasks you'd normally use JSON or object serialization for, all the way up to large distributed clusters.

Because the native object model is mapped directly to storage there's no object/relational mismatch.

Re: ORMs are nice but they are the wrong abstraction

#29

The most interesting/fresh approach I've seen to this problem is Permazen. https://github.com/permazen/permazen/ It starts by asking what the most natural way is to integrate persistence with a programming language (Java in this case, but the concepts are generic), and then goes ahead and implements the features of an RDBMS as an in-process library that can be given different storage backends as long as they implemen…

.. but you can't really do queries on a K/V store?

Re: ORMs are nice but they are the wrong abstraction

#30

The most interesting/fresh approach I've seen to this problem is Permazen. https://github.com/permazen/permazen/ It starts by asking what the most natural way is to integrate persistence with a programming language (Java in this case, but the concepts are generic), and then goes ahead and implements the features of an RDBMS as an in-process library that can be given different storage backends as long as they implemen…

DPP (Deep Persistent Proxy Objects) is a javascript library starting from the same question:

https://github.com/robtweed/DPP

Post reply on HN