Live data from Hacker News

To ORM or Not to ORM

eli.thegreenplace.net

21–30 of 300 posts

Re: To ORM or Not to ORM

#21
I've wanted to like ORMs but they always get in the away as requirements start to get more complex. On the other hand, I like writing in plain SQL, but that's often hard to comprehend by other people - you cannot just glance through to know what's happening under hood.

I find Ecto to be the perfect balance of expressiveness, flexibility and clarity. In fact it's the single reason to have picked up Elixir.

Re: To ORM or Not to ORM

#22

One thing I really miss from the JVM world was something like jOOQ. I don't want an ORM (like specifically the object relational mapping stuff) for most of my use cases but I do want an abstraction above text for interacting with SQL. gorm's sql builder is alright but something better would be really nice.

psycopg2 is good for this in python.

psycopg2 is a Postgres driver, it is not a query builder like jOOQ. Python's equivalent would be SQLAlchemy Core.

Re: To ORM or Not to ORM

#23

Both... I prefer a hybrid approach when it comes to ORM's. In most projects I will use an ORM to avoid boilerplate for simple entity queries (save, findByid, findByX, delete) and converting a database row to a class instance but I avoid mapping complex relations. An ORM like Spring Data with JPA/Hibernate in the Java/Kotlin world works well for this. I write my db schema by hand, create an entity class with the same…

Yep, the Spring Data CrudRepository interface based stuff is an amazing time saver. Just extend an interface, and BOOM, you've got the basic CRUD operations ready to go. And then you just use HQL in annotations for more specialized queries. Other people's mileage may vary, but I've found this to be a tremendous boon.

Re: To ORM or Not to ORM

#24

Sample size one 1 but ORM drives me crazy. Why can't we just use SQL? How does it save time when I have to learn the ORM language, which probably has a lot less support and users? The OP here says it "reduces boilerplate" -- rarely have I created an application and thought its biggest problem was too much boilerplate. But everyone at work loves them so I must be wrong somehow.

I think that our love/hate for ORMs stems from the 80/20 rule.

For 80% apps/workloads/programmers, ORMs are pretty helpful (in that they save you computation time, thinking time, etc).

For 20% of cases, the ORM tends to get in the way. In these cases, the ORM doesn't prevent you from needing to know SQL, it doesn't give you performance gains, and it actively obfuscates what's "really going on". These cases make your job harder, and are what stick up in your mind when you think about ORMs.

At my place of work, we use an ORM (SQLAlchemy), but codegen the "annoying/boring" bits of ORM code (model/class specification, table/index creation). IME, it's wonderful and good – until I start wondering about how much memory this query is going to take up, 5 minutes after the client has made it. I know that SQLAlchemy probably caches things, but I have no idea how it's cache policy is set. When will my query be garbage collected? How much memory should I request for my app?

I can probably find this out by digging into the SQLAlchemy docs, but I haven't yet so uncertainty looms. TFA's "I'd much rather spend an extra bit of time typing, but this will save me time reading ORM's documentation, optimizing my queries, and most importantly debugging" rings true to me.

With raw SQL, answering these questions might be more straightforward – but I only ask these questions 20% of the time! Anyways, there's some food for thought. :)

Re: To ORM or Not to ORM

#25

    rails g model Post published_at:datetime title content:text
    rails g model Comment post:references author published_at:datetime content:text
    rails g model Tag name
    rails g model PostTag post:references tag:references

    class Post 
from there it's just regular rails:

    Tag.find_by(name: "something").posts
    Post.joins(:tags).where(tags: { name: "something" })
    Tag.create(name: "something")
    Post.create(...)
    Tag.first.posts 
made a little repo if people want to play with it: https://github.com/localhostdotdev/bug/tree/orm-or-not-orm

Re: To ORM or Not to ORM

#26
I like the Django ORM. This is likely for two reasons:

1. I only use Django for small use cases where I rarely see any sort of scope creep. There was no real conscious decision about this, just kind of the way it happens.

2. The Django ORM is fairly mature and makes it quite easy to get a small project out the door.

I regularly use SQL directly at work and wouldn't want to try to replace any of it with an ORM even on new projects because we are all just used to working with it and we wouldn't want to add any complexity to our workflow without real, tangible benefits.

I would not, however, be opposed to working on a larger Django project (ORM and all) if the opportunity presented itself.

Re: To ORM or Not to ORM

#27

Sample size one 1 but ORM drives me crazy. Why can't we just use SQL? How does it save time when I have to learn the ORM language, which probably has a lot less support and users? The OP here says it "reduces boilerplate" -- rarely have I created an application and thought its biggest problem was too much boilerplate. But everyone at work loves them so I must be wrong somehow.

In my experience an ORM will cause issues if used thoughtlessly throughout an application. Objects created by an ORM can be used "naively" and cause many round trips to the database, very inefficient queries, etc., but they boilerplate they can save is really helpful also.

I worked on a simple Python CRUD app project a few years ago with some folks who were absolutely against using an ORM for those reasons. There was so much code written that basically just took tuples returned from the DB and instantiated Python objects with them (and vice versa for inserts) that making changes to anything was absolutely maddening. I find that SQLAlchemy Core can be used very simply to essentially take a DB query and return Python objects.

Re: To ORM or Not to ORM

#28

Sample size one 1 but ORM drives me crazy. Why can't we just use SQL? How does it save time when I have to learn the ORM language, which probably has a lot less support and users? The OP here says it "reduces boilerplate" -- rarely have I created an application and thought its biggest problem was too much boilerplate. But everyone at work loves them so I must be wrong somehow.

I am very happy with ORM.

I'm currently working on an e-shop. I need to display many tables with different filters.

Administrator wants products starting with string? OK: if filter['category']: qs = qs.filter ... Administrator wants products without category? Ok: if filter['category']: qs = qs.filter ... Add multilang supprt? Ok: subclass model, mark fields and translatable, run migrate script

I can combine any filters, ORM automatically creats query with necessary joins, subqueries, group by expressions etc.

If i didn't use ORM i would have to write code that generate select with cca 200 ifs because almost every filter can generate join or add fields to group by or generate subselect etc.

Re: To ORM or Not to ORM

#29
post #22

Earlier quoted context omitted.

psycopg2 is good for this in python.

psycopg2 is a Postgres driver, it is not a query builder like jOOQ. Python's equivalent would be SQLAlchemy Core.

I was referring to the string composition [0] of psycopg2, a library which also takes care of converting SQL results to the correct Python data type. IMO that meets the criteria for an "abstraction above text" that GP was asking for.

But yes, SQLAlchemy Core is far more full-featured and gives you more than just a few primitives to work with. It also has a lot of library-specific constructs and is much closer to a full-fledged "ORM" than a query builder; what you're writing is not exactly SQL and is yet another dialect to learn.

[0] http://initd.org/psycopg/docs/sql.html

Re: To ORM or Not to ORM

#30
As true now as it was 12 years ago: ORMs are the Vietnam of computer science. http://blogs.tedneward.com/post/the-vietnam-of-computer-scie...

(tl;dr: there is an impedance mismatch / leaky abstraction between relational data and OOP for all but the most trivial use cases, guaranteeing that whatever problems an ORM might solve will be traded for new ORM problems instead)

Post reply on HN