Readit News logoReadit News
blagie · 2 years ago
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-tier architectures, so mostly it's misused. There is an attempt to hide them.

3) ORMs generally make easy stuff easy, and hard stuff painful.

What I generally want is:

1) Something translating SQL syntax into my native language, but maintaining SQL full semantics and expressiveness.

2) This should allow me to be database-agnostic.

3) This should prevent things like injection attacks.

4) It should not map onto objects. It should maintain 100% of the capability of SQL. The only differences should be syntactic (e.g. instead of writing WHERE, I might write .where() or similar).

5) This may and ideally should have added functionality, for example, around managing and organizing database migrations.

Stored procedures and virtual tables are also very important (but poorly implemented in most databases). These:

1) Allow proper abstraction at the SQL level.

2) Improve performance.

What most programmers fail to understand -- since universities don't teach -- is how powerful and elegant the underlying theory of databases is.

snorremd · 2 years ago
My best experience integrating with a Postgres database was using Clojure with the Honey SQL library [1]. Essentially Honey SQL operated solely as a query builder library allowing me to express queries as regular Clojure EDN maps and vectors (comparable to objects and arrays in JavaScript), or nested function calls returning the same kind of data. In essence SQL queries were expressed in a Clojure native data format which finally could be transformed into SQL syntax.

The huge win was that I could use the Clojure REPL to run my SQL-generating functions and see that the SQL actually matched what I was aiming for. Caching the query-building was as simple as memoizing the query building functions as queries are parameterised in any case, so can be reused.

There is something really nice about having the full power of SQL at your fingertips. The only drawback compared to ORMs of course is that it outputs the resulting query result in a flat structure. So any aggregation you'd need to handle in code or use something like Postgres JSON aggregation.

[1] https://github.com/seancorfield/honeysql

sureglymop · 2 years ago
I learned about database theory a few times (and even interned as a DBA before I started uni). But it wasn't until my discrete math class in university that I finally understood, "oh, that's why we call a table a relation, a row a tuple, etc", and from there - started understanding the whole relational model and why it makes sense.

I think ORMs however, became a thing more so due to fear of having to write (correct) SQL. It was meant to reduce complexity for developers but of course that's not really how it panned out in reality.

everforward · 2 years ago
https://GitHub.com/masterminds/squirrel sounds like what you want, in Go at least. They bill it as a SQL query builder instead of an ORM.

It lets you build SQL queries using code and just returns the built SQL query as a string, the list of variables to pass into the query, and an error if your query was invalid.

Actually executing the query and doing stuff with the result is explicitly outside their scope. I typically use it with sqlx with a struct per query, just to avoid writing tedious row iterators.

Makes it super easy to eg define a function that adds paging to a query and returns the paged query. You can even write a function that takes an HTTP request, reads out standard paging parameters and adds them to a query.

pjc50 · 2 years ago
> 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.

blagie · 2 years ago
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.

mike_hearn · 2 years ago
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.

PH95VuimJjqBqy · 2 years ago
> Most people never learn databases, and most people who do are idiots working on enterprise three-tier architectures

Tell me how you really feel :)

My biggest problem with ORM's is that it causes people to sprinkle the ORM code throughout their codebase but they'd never do that with SQL, they'd want to try and sequester it to a system whose responsibility is the retrieving and updating of data.

It puts people into a poor mindset around their data.

feoren · 2 years ago
That's like saying: my problem with Functions is that people sprinkle them throughout their codebase, instead of having one single place where all their Functions are defined.

For most applications, "Retrieving and updating data" is the entire point, or at least a deeply fundamental part of how they work. It shouldn't scare you that such a core function -- the entire reason your application exists -- is "sprinkled around" your codebase. It should scare you if it isn't! I think the reason it scares you is because you imagine the performance nightmare of every random function possibly hitting the database N times. That's using an ORM terribly. Good ORMs let you plan database actions, and compose those plans.

Guys, you really, genuinely, don't have to choose between (a) writing SQL strings in code, and (b) using an ORM badly. You can truly do better!

brainlessdev · 2 years ago
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.

blagie · 2 years ago
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: ...

random_kris · 2 years ago
https://orm.drizzle.team/ Basically this ?
iccananea · 2 years ago
Drizzle does exactly what I described in the article: it re-implements SQL in the target language (in this case TypeScript).
feoren · 2 years ago
> What I generally want is:

> 1) Something translating SQL syntax into my native language, but maintaining SQL full semantics and expressiveness.

You have just described a good ORM used well.

> 2) This should allow me to be database-agnostic.

Meh, you sacrifice some powerful features if you demand total database agnosticism, and how often do you actually switch databases? Being database-agnostic is a side benefit of writing your logic simply against a good abstraction. The biggest benefit is composability. You can write (and optimize) one query against abstractions, and re-use that query in lots of different ways.

> 3) This should prevent things like injection attacks.

As all ORMs automatically already do.

> 4) It should not map onto objects. It should maintain 100% of the capability of SQL.

If it's not mapping onto objects, what is it doing? The problem is that your mental model of what "objects" means includes awful design decisions like deep inheritance trees, mutability, and lots of reference cycles (this.parent.child[0] == this, etc.) If your object model is already clean and following relational principles, then mapping into that model is exactly what you want.

It should not strive to maintain the capability of some bastardized pseudo-language which is despised by the progenitors of relational logic. It should strive to support the relational model. That's not the same thing.

> 5) This may and ideally should have added functionality, for example, around managing and organizing database migrations.

No, because your code versions and your database schemas advance together. To reliably run a database migration in code, you'd need to run it with the code version that exactly matches each step in the schema. That means for each step in the migration, you'd need to checkout the correct commit in git, compile, and run the ORM code in that version. Either that, or you're maintaining a code model that is compatible with every historical database schema, which is way worse.

But what ORMs should be able to do (and I haven't found one that does this well) is generate SQL migration scripts for you, which you store. Those would be frozen relative to the database schema version, so all the above problems go away.

> What most programmers fail to understand -- since universities don't teach -- is how powerful and elegant the underlying theory of databases is.

The underlying relational model is powerful and elegant. SQL itself is not. SQL is disliked by the founders of the relational model. Good ORMs let you incorporate the relational model into your code.

blagie · 2 years ago
> You have just described a good ORM used well.

No. I did not. ORM is an "object–relational mapping." It maps data relations onto objects.

https://en.wikipedia.org/wiki/Object%E2%80%93relational_mapp...

> how often do you actually switch databases?

Very often. Virtually all of the systems I write have at least two back-ends to maintain that flexibility. At the very least, I'd like my systems to run well locally for development but also to scale. The easiest way to do that is to e.g. support both SQLite and Postgres, but there are other ways which make sense.

In proprietary settings, I like having a BATNA. The architectural flexibility means I get better prices on hosted services and aren't liable to turning into a cash cow through lock-in. That's savings even if I'm not switching.

> If your object model is already clean and following relational principles, then mapping into that model is exactly what you want.

This is where your thinking broke. A good object model is NOT a relational model, and a good relational model is NOT an object model.

Learn the theory of both. They're both good theories, but they're different.

An ORM makes sense if you want to use an object model, but want the backing store to be an RDBMS.

> But what ORMs should be able to do (and I haven't found one that does this well) is generate SQL migration scripts for you, which you store. Those would be frozen relative to the database schema version, so all the above problems go away.

I believe that's one instantiation of what I wrote: "This may and ideally should have added functionality, for example, around managing and organizing database migrations." It's actually exactly what I was thinking.

Some ORMs do this not badly, actually. Don't let the perfect be the enemy of the good. A simple system which does 90% of the work of generating a migration (with manual verification and tweaks) is often better than a complex one which tries to do 100% of the work.

> The underlying relational model is powerful and elegant. SQL itself is not. SQL is disliked by the founders of the relational model.

Citation required.

In either case, ORMs aren't translating just syntax, but also semantics. That's where the problem lies. If you're not doing that, you're not an ORM.

> Good ORMs let you incorporate the relational model into your code.

You're confused about what an ORM is. ORMs essentially map an RDBMS onto an object model (which an OODBMS does natively). The two models are fundamentally different. It's a translation layer.

Any good database library will let me "incorporate the relational model into my code." That's not an ORM.

mike_hearn · 2 years ago
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.

amadeuspagel · 2 years ago
DPP (Deep Persistent Proxy Objects) is a javascript library starting from the same question:

https://github.com/robtweed/DPP

pjc50 · 2 years ago
.. but you can't really do queries on a K/V store?
mike_hearn · 2 years ago
Sure you can. In Permazen they're expressed as set operations over collections of objects, so you use map, filter, fold etc as you would if programming functionally. Indexes are also exposed as collections.

Reads and writes on those objects are then mapped to K/V operations.

jasfi · 2 years ago
ORMs are useful when what you want to do matches their expressiveness. For example when I just want to get a record by its primary key. They have their limits, many times when doing complex reports. For that, most ORMs provide a raw SQL function. So just use the right tool for the job.

Stored procedures for business logic can be great for performance when they replace queries/mutations called thousands+ times.

blagie · 2 years ago
ORMs are nice for "Hello World," and hit a brick wall later.

I don't mind them for simple systems, but for more complex systems which need SQL, don't use an ORM. Mixing ORMs with raw SQL generally mixes and breaks layers of abstraction. This leads to a situation where the overall system is more complex than having a sane relational abstraction. For example, ORMs do a lot implicitly. A code change at the language level, with something like the Django ORM, can and will break your SQL code. Your data logic is also split across two places.

The only time I've seen this work is for SQL for one-off analytics done at a command line, where the SQL code is never intended to be reused.

EITHER:

- Use an ORM, if you are building something simple like a todo list or a basic eCommerce web site; or

- Use full SQL if you are doing something more complex (e.g. if you ever expect to do analytics); or

- If your programmers don't understand SQL, consider whether you want an ORM, a nosql, or to find programmers better matched to the problem domain.

(Footnote: Many good programmers don't understand SQL; that's okay. They just shouldn't be designing databases, any more than e.g. database experts should be designing machine learning algorithms, or machine learning experts should be designing front-end user interfaces. They're different skill domains, and they're all equally important. The key thing is people should know their skills. Otherwise, you'll get an unusable UX, a regression as our ML model, and a broken schema. This is especially true for something which looks simple on the surface but has deep theory behind it. ORMs make it look easy.)

jasfi · 2 years ago
Then you'd never use an ORM, because you might require something an ORM can't handle at some point. I don't see why a coder couldn't handle a mix of both. The other factor is that ORMs tend to add features over time. Something that would require raw SQL today might be more elegantly handled with an ORM's language two years from now.
kaba0 · 2 years ago
If you don’t know SQL, you shouldn’t touch ORM either, no one ever said that you can.

Also, it’s pretty reductionist to say that it is only good for simple systems, when in fact, huge services are running it at almost all companies.

TacticalCoder · 2 years ago
Here's a 2006 blog commenting on (and agreeing with) a blog entry from 2004 which I vividly remember: "ORM is the Vietnam of Computer Science"...

https://blog.codinghorror.com/object-relational-mapping-is-t...

DrDroop · 2 years ago
At this point it is more accurate to call ORMs the Afghanistan of Computer Science, you where warned that it was going to be like Vietnam and somehow you managed stayed there even longer.
hardware2win · 2 years ago
Too much ORM hate in this thread

They are really useful for like 90% of the work

The rest can be made with raw sql

Combine strengths of those two powerful tools, dont be religious

selfportrait · 2 years ago
This here. If you follow Prisma ORM on GitHub, some of the pain you’ll see is missing features like “whereRaw”, but really most of the pain is forcing you to use raw SQL. And even then, Prisma is extensible, so build your own solutions on top of it. Like Zenstack which creates auth/role permissions on the Prisma schema.

Way too much ORM hate here.

mewpmewp2 · 2 years ago
Couldn't agree more and I just recently had few comments on the topic.

Everytime I use ORMs I feel frustrated by the capabilities compared to raw sql.

I feel handicapped in being able to have the data in a way I want.

jovezhong · 2 years ago
SQL is so flexible. When I put myself as a user, I also prefer those tools that provide nice UI for filter/reporting, but also give me SQL interface to do advanced query, such as PostHog, Resmo, JIRA(?)
kaba0 · 2 years ago
ORMs never block you from issuing raw SQL queries. But mapping the results to entities inside your programming language’s abstraction is a repetitive task, ready to be abstracted away. The reverse direction is the same way.

I feel most of the criticism of ORMs come from people who don’t actually know how to properly use one. They were never meant for OLAP, they are for OLTP.

mewpmewp2 · 2 years ago
Maybe I don't know how to use one properly, but I have used them for years. For me however tech should be learnable quicker to be beneficial.

Now I only use ORM really only for some basic CRUD queries, if that.

Taikonerd · 2 years ago
The DB/ORM mismatch is worse than it has to be, because SQL queries always return flat rows. But in code, we don't usually want rows; we want objects linked to other objects: "get me the Account objects and their associated Wishlists."

If you use a query language that knows that we want objects linked to other objects, you can still have an ORM, but it doesn't have to do as much heavy lifting. The query can already specify the objects and properties you want explicitly, so you don't need to worry about, "which properties of which objects do I flesh? Am I over-fetching?"

That's why I've become a booster of EdgeDB: https://www.edgedb.com/ It's a sort of "midpoint" between regular SQL and an ORM. And it's language-agnostic, unlike an ORM.

1st1 · 2 years ago
Yeah, that's the exact goal of EdgeDB -- unlock proper hierarchical data retrieval and mutation as well as making composition possible both at the schema and query layers. Most other perks EdgeDB has are the direct result of that.
ravenstine · 2 years ago
I dislike ORMs because they make big promises in exchange for giving up comtrol. I know this is one of those topics where someone may tell me I'm wrong and that I should be using some other ORM that truly does everything right. In my experience all ORMs have shortcomings and ultimately get in the way more than just writing SQL and constructing objects yourself, even though they can save time upfront.