Hacker News new | past | comments | ask | show | jobs | submit login

Write raw SQL queries. My feeling on the Django ORM is that it's useful for defining models and managing migrations, but I prefer to write raw SQL for all queries.

The PostgreSQL documentation is really good too.




Meh. My feeling is that Django and other web frameworks are too magical -- I always worry they might be doing something inefficient. That's why I write all my web applications in hand-tuned assembly. It even has reusable abstractions for when I need them, and nice hand-formatted assembly is super readable!


You're seriously comparing SQL to assembly? There's a point where it doesn't make sense to add another layer, another dependency, etc.


In both the "never use an ORM" and "always use assembly" cases, the argument is based entirely on having full control of what's going on and not trusting any intermediate layer to get it right or eke out every last femtosecond worth of performance.

So it's a perfectly fair comparison.


The ORM takes a whole lot of pain out of updates as well.

Filtering also work fairly nicely for simple stuff, as putting conditions in a dictionary and passing it to filter(kwargs) is a lot nicer than messing about with SQL strings.

I agree that the ORM is limited if you want moderately complex queries though.


> but I prefer to write raw SQL for all queries.

If you're doing this you're missing out on the biggest advantage of the ORM, which is that it allows you to compose queries.

Using the Q objects, query expressions, and custom Queryset objects, you can filter objects by pretty much every imaginable criteria out there. This would be horrendously difficult and error-prone writing plain SQL.

The Django ORM, despite having limitations if you want to run analytics (and that's really a use case for with plain SQL excels), generally writes out exactly the same SQL I'd write by hand.


> This would be horrendously difficult and error-prone writing plain SQL.

I do it all the time :) I love looking at long SQL queries that are formatted nicely and use good naming conventions.

It's also part of a more general philosophy I have of reducing dependencies whenever possible. With Django it makes sense to use the ORM wherever it suits you, since it's already built in, but there are times when you need to know raw SQL anyway (e.g. try populating a large database without COPY, only using the Django ORM).




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: