Live data from Hacker News

Don't use your ORM entities for everything – embrace the SQL

blackparrotlabs.io

11–20 of 48 posts

Re: Don't use your ORM entities for everything – embrace the SQL

#11
My simple rule for databases is that if you are not actively querying on it, it doesn't need to be a separate table or column. Use the YAGNI rule here and you'll be better off. The classic example here is persons that have addresses and phone numbers. Most applications have no requirements to query on most of that: street name, postal code, phone number, etc. Less tables and columns mean simpler joins (or better, no joins at all). Any structured data that you don't query, just store it in json form in a blob and simplify your schema. Anything that actually requires complex querying, consider using a proper search engine. Or extract it to a dedicated field with some index on it that actually helps you querying effectively. The golden rule here is that if that query is connected to user input you probably need some notion of ranking, fuzzy searches, etc.

Either way, everything gets easier with a simple schema. Faster query and insert performance, easier to reason about when doing transactions, easier to maintain, etc.

Re: Don't use your ORM entities for everything – embrace the SQL

#12
post #5

The article mentions database portability as an advantage of ORMs, and I agree - to me that is basically the only real advantage to ORMs. The article also mentions that database portability is not a very compelling advantage, since lots of applications don't actually need that, which I also agree with. Personally I just pick either PostgreSQL or SQLite depending on my use case (and they're different enough that there…

Postgres is the one time I embrace lock-in, if the topic of switching to mysql or something else comes up it's a good thing if there'd be a prohibitive amount of work involved.

Re: Don't use your ORM entities for everything – embrace the SQL

#13
> Will you actually need to change between fundamentally different database technologies?

Yes, use sqlite locally for development and run PG in prod. Unit tests can now use the db and finish in milliseconds. You get unit tests that have the power of integration tests and don't have to ever stub out your db. I use Redislite for the same thing.

I'm of the opinion that SQLite is the musl of the SQL world. By deciding that you'll support it first-class you'll avoid the sharp edges of database specific behavior and extension hell and write better more maintainable code.

"Sorry we can't actually put that logic the database, SQLite doesn't support spooky action at a distance."

Re: Don't use your ORM entities for everything – embrace the SQL

#15
As someone who always prefers native SQL over ORM queries, I'll add a couple of counterpoints.

Most IDEs provide intellisense/validation of ORM entities, vs treating SQL like a raw string.

ORM entities also make refactoring and impact analysis slightly easier.

Despite those benefits, I generally find ORMs a pain for anything besides the most basic queries.

Re: Don't use your ORM entities for everything – embrace the SQL

#17
post #6

I still don't understand why people believe that abstracting away something as important as your data store is a good idea. They quickly run into performance problems that are quite difficult to fix. It's funny that the same developers that try to avoid vendor lock-in don't realize they've locked themselves into an ORM forever. Plus, SQL isn't that hard. And as a backend person, you should be able to visualize your d…

We use Kysely[0], which is a nice balance. The code you write in Kysely translates 1:1 into SQL, which means there's very little perf left on the table, but we get an interface that's automatically typed, type safe, and works well with our js language tools. [0]: https://kysely.dev/

Kysely is excellent! Another library that complements it is pgTyped[0]. It compiles .sql files into functions with strongly typed parameters and return values

[0] https://pgtyped.dev/

Re: Don't use your ORM entities for everything – embrace the SQL

#18

Comments here make me wonder if I've just been spoiled by ActiveRecord. Not that I use it for all queries but 1) it's rare that I have to resort to raw SQL and 2) it kindly gets out of the way when I do.

> resort to raw SQL

I'm the opposite, I would rather write SQL than "resorting to" ORM queries, which is why my favourite libraries are aiosql[1] in Python, Hugsql[2] in Clojure and similar: write the queries as SQL in .sql files, which then get exposed as functions to your code.

[1] https://nackjicholson.github.io/aiosql/

[2] https://www.hugsql.org/

Re: Don't use your ORM entities for everything – embrace the SQL

#20
In the 1980's we had C/C++/Ada/Pascal/Fortran/Assembly/Lisp. Since the 1980's the languages have exploded.

But we still only have one language for querying a database. And a kinda not very good one at that. Why is that?

https://www.holistics.io/blog/quel-vs-sql/

One could argue that ORM's are the alternative language people are looking for.

Post reply on HN