News for hackers. Stuff that matters.
Re:What ORMs Have Taught Me: Just Learn SQL (2014) by lcrz on Wednesday October 21, 2020 @03:35AM (#24845587)
Counterpoint: a lot of ruby devs are very familiar and capable with ActiveRecord. Al lot of python devs are familiar and capable with using the Django ORM. Why are these ORM more likely to limit your application? You provide no proof or explanation of your statement.
Re:What ORMs Have Taught Me: Just Learn SQL (2014) by darkhorse13 on Wednesday October 21, 2020 @04:02AM (#24845713)
I second this. For example, Django's ORM is actually very advanced these days, allowing you to squeeze out the performance in many, many cases. While I agree that ORMs should not always be used (sometimes you just need raw SQL, especially on very large databases), I think the ORM hate is a bit overstated on HN.
Re:What ORMs Have Taught Me: Just Learn SQL (2014) by metafunctor on Wednesday October 21, 2020 @05:10AM (#24846059)
The Django ORM is pretty good, and I like it. But, to squeeze out performance, wouldn't you say you have to actually understand what is going on in the database?
Re:What ORMs Have Taught Me: Just Learn SQL (2014) by BozeWolf on Wednesday October 21, 2020 @08:28AM (#24847095)
Agree! I like django's ORM and I love SQL databases. It is nice that your in-code model changes, will reflect the schema changes in the database. And migrations are a blessing. (Of course it has some foot guns.)

For optimizations: my_queryset.explain(verbose=True, analyze=True) is very nice to understand what is going on. If you cannot get it done with django's database abstractions, there is always:

my_queryset.extra() or even my_model.objects.raw(). Where the last one maps the result to the model, for free.

To others responding that ORMs are bad: IMHO saying that raw sql is better than ORMs is short sighted. Many simple things are nice in ORMs. Queries are often simple. Today, in django, complex queries are also nice. For edge cases raw sql can be better.

Extending queries is also super easy with a good ORM, where with raw sql you would have to maintain two almost the same queries.

Just remember to run and inspect queryset.explain().

Re:What ORMs Have Taught Me: Just Learn SQL (2014) by darkhorse13 on Wednesday October 21, 2020 @03:29PM (#24851168)
Fully agree with this. These discussions are always this or the other, but you can easily have both in the same project. Django's ORM and raw SQL functions prove that.
Re:What ORMs Have Taught Me: Just Learn SQL (2014) by sjwright on Wednesday October 21, 2020 @05:59AM (#24846263)
And more importantly, you'd want to design your database to be optimal for complex SQL queries and not whatever generic structure that matches your programming objects.
Re:What ORMs Have Taught Me: Just Learn SQL (2014) by darkhorse13 on Wednesday October 21, 2020 @03:25PM (#24851129)
I don't see why those things have to be mutually exclusive. You can understand your database and still use an ORM.
Re:What ORMs Have Taught Me: Just Learn SQL (2014) by metafunctor on Thursday October 22, 2020 @03:37AM (#24855736)
Yep. My point is that if you use an ORM, but don't understand your database, you're asking for trouble.
Re:What ORMs Have Taught Me: Just Learn SQL (2014) by ClikeX on Wednesday October 21, 2020 @03:58AM (#24845691)
The only limiting thing I've experienced with ActiveRecord is when the database becomes larger and more complex. The performance takes a bit of a hit. Which isn't ActiveRecords fault, it's the developer's lack of understanding how the database works.

Most of the issues I see is a lack of understanding of things like N+1 queries. I see this mistakes from junior to senior level. ActiveRecord has built in solutions to fix them too. Some developers don't know about them.

Re:What ORMs Have Taught Me: Just Learn SQL (2014) by yxhuvud on Wednesday October 21, 2020 @04:18AM (#24845800)
There are built-in solutions but also quite severe limitations when you get to advanced usage of activerecord. Especially with regards to composability. Of course, raw SQL is also really bad in that scenario.
Re:What ORMs Have Taught Me: Just Learn SQL (2014) by tylerhou on Wednesday October 21, 2020 @05:36AM (#24846164)
When I found ActiveRecord insufficient, I found it easy to drop down to Arel for complex queries: https://www.rubydoc.info/gems/arel
Re:What ORMs Have Taught Me: Just Learn SQL (2014) by bdcravens on Wednesday October 21, 2020 @05:18AM (#24846087)
Working on a Ruby application that uses a database that predates the application (it was originally written in another language), there are simply some things that can't be written in AR, or when they are, the performance just isn't there.

Additionally, we often have to create ad-hoc queries to answer questions from stakeholder. It's easy enough to just pop up DataGrip and write the query, and copy the tabular results into a doc in Google Sheets for sharing.

I think the best approach is ORM-first, not ORM-only.

Re:What ORMs Have Taught Me: Just Learn SQL (2014) by WJW on Wednesday October 21, 2020 @06:35AM (#24846442)
An alternative viewpoint on that situation might be that the database is in need of some serious refactoring/migrating if the app can't deal with its structure. Trying to bend Rails into non-AR database usage is going to lead to tears sooner rather than later IMO. It's just so very integrated.

Having separate applications with (write) access to the same database is similarly going to end in trouble. Suddenly there are all sorts of problems when the database needs a migration and the applications are expecting different things from the same table. This is not really ORM-related btw.

That said, ActiveRecord is not perfect and sometimes a single handcoded SQL query can deliver a ton of value. Profile carefully.

Re:What ORMs Have Taught Me: Just Learn SQL (2014) by dragonwriter on Wednesday October 21, 2020 @09:47AM (#24847823)
> Trying to bend Rails into non-AR database usage is going to lead to tears sooner rather than later IMO. It's just so very integrated.

Is that really true? While I've barely used Rails in anger, and not at all recently, I was under the impression that all the major Ruby ORMs and high-level database libraries (e.g., DataMapper, Sequel) are heavily focussed on Rails usage and usually around AR pain points like working with DBs in Rails that don't fit ARs opinionated patterns.

> Having separate applications with (write) access to the same database is similarly going to end in trouble.

It shouldn't, if the DB is designed for that, which usually means each application sees the DB exclusively through its own set (probably in a distinct schema) of views, and keeps it's filthy little hands off base tables. That's a best practice for multiple application access to an RDBMS that is older than the Web.

Re:What ORMs Have Taught Me: Just Learn SQL (2014) by bdcravens on Wednesday October 21, 2020 @02:37PM (#24850711)
I won't argue that an ideal situation is to stop all feature development and refactor into an ideal data model. That's a technical debt ROI discussion, the kind that we always have, and sometimes the business value just doesn't support the rewrite.

However, even in an ideal data model, I don't always agree that multiple applications accessing the application's database is bad. That's a matter of being thoughtful and communicative, instead of just wantonly running "rails g migration" every time you need to implement a feature (that's true of Monorails even). FWIW, we've never had any real issues with multiple applications accessing our single database.