Python ORMs (SQLAlchemy, etc)

Status
You're currently viewing only w00key's posts. Click here to go back to viewing the entire thread.
Not open for further replies.

w00key

Ars Legatus Legionis
10,099
Subscriptor
Yea, with an ORM, unless you use MySQL/PostgreSQL/SQLite specific features, your app will work on any of them. Plus, I prefer to think in terms in my app and let the ORM and database figure out how to get the data. For example, the following query:

Code:
Story.objects.filter(published=True, tagRelations__tag__id=1).order_by('-creationDate').select_related('text')[:10]

would be a select with an join on TagRelation, a simple where and order by clause, plus an optional join following the Story.text relation to eagerly fetch the field. It's much easier to use eager fetching of related fields like this instead of more JOIN ON (w.text_id = t.id) clauses and unpacking the rows manually.

Oh, each of those query methods returns a new QuerySet, so you can further manipulate, filter, pass it to functions, etc etc before you finally execute it. It beats messing with strings if you need to add things in the WHERE clause.


Another bonus with Django is the shell; add ipython, and you'll have one heck of a management console, with auto complete, debugger, profiling and script editing support. Most things that can't be done in the web based management is often just one or two queries away.

I haven't looked at SQLAlchemy yet, but from what I've heard, it's even better. For now, we're sticking with Django - if it isn't broken, don't change it. The arcane stuff must be done in plain SQL anyway - mostly maintenance queries to update a good chunk of the database in a single pass with an insane update select from + multiple nested subqueries + windowing functions (PSQL 8.4 only). Those run way faster than a plain .filter() + .update() combo. No ORM will save you from coding those.
 
Status
You're currently viewing only w00key's posts. Click here to go back to viewing the entire thread.
Not open for further replies.