Python ORMs (SQLAlchemy, etc)

Status
Not open for further replies.

drogin

Ars Tribunus Angusticlavius
8,283
Subscriptor++
I'm fairly well experienced with .NET development on Windows, but there are a couple of side projects I'd like to use as an opportunity to learn Python and its ecosystem.

I've gone through a book and gotten a handle on the basics of the language itself (which still feels kind of dirty to me) and no I am looking into libraries to provide some services I'm used to on the .NET platform.

One of those things is a good ORM. I've used Subsonic, NHibernate, and Entity Framework in .NET and I am looking for something with the same set of capabilities. I've been reading about SQLAlchemy and it seems like the most mature framework. However, if anyone here is more plugged in to the Python universe I'd appreciate hearing about newer or better systems.

At a high level:

I tend to favor ORMs that allow me to separate my mapping (i.e. meta) information from my domain models.

I prefer Data Mapper style ORMs over Active Record, but I would be willing to give Active Record another chance if the library was "OMG Awesome".

The ORM should have a fluent API for queries.

The ORM should generate at least somewhat performant SQL.
 

falcolas

Ars Scholae Palatinae
748
SQLAlchemy is likely your best bet, but it's not as mature as many of the ORMs for other languages; it has a number of limitations that make it less than ideal for managing corner cases.

For any project with medium to high complexity, I would almost recommend rolling your own (python makes this fairly simple) that works with the project you are trying to do, and with your requirements.

Good luck!
 

FogleBird

Ars Tribunus Militum
2,237
Another vote for SQLAlchemy.

For a while I used Django's ORM even outside of web applications (just because I was familiar with it) but recently took the plunge and learned SQLAlchemy, and it was awesome. Definitely better and more flexible than Django's ORM yet still easy to use. Seems way nicer than when I tried to use Hibernate with Java some years ago. (Probably mostly due to Python's flexibility.)

I disagree with falcolas regarding rolling your own.

And Python isn't dirty! You've just been brainwashed all these years. ;)
 

Xavin

Ars Legatus Legionis
30,828
Subscriptor++
Rolling your own is almost always the wrong solution for something like an ORM. If one doesn't exist that does what you want or can be bent to do what you want, then it's time to think about switching languages.

It sounds like you are talking about small personal projects though, so pretty much anything goes for that. Rolling your own ORM is probably something you will only ever want to do once though ;)
 

drogin

Ars Tribunus Angusticlavius
8,283
Subscriptor++
FogleBird":3dnw30k6 said:
And Python isn't dirty! You've just been brainwashed all these years. ;)

LOL. At this point my statement is mostly just do to a couple of things:
  • The whole module/package thing creeps me out. I'm used to dealing with "hard" entities like assemblies and dlls when it comes to making reusable code. It just seems all very wrong to base something like that on naming conventions. If it is a directory it is a package. If it has a file called __init__.py then you can restrict imports by having a magic variable __all__, etc.
  • The fact that you can use some kind of crazy syntax to bake unit tests into your documentation. Part of me thinks this is really cool, the rest of me thinks :scared:

Again, these are just some mindshift things. It is obviously a successful system.
 

bk0

Ars Tribunus Militum
1,801
Not to hijack the thread, but I'm currently implementing a db app in Python and (naively) never considered any ORM type stuff. What is the advantage of this over coding straight SQL into the DB handling classes? For example, will SQLAlchemy auto-generate SQL for different backends so I could hook up a MySQL backend at some point in addition to the current sqlite backend? Or does this allow some version of schema-independence when writing the db handling code?

Sorry if these are dumb questions, I'm hacking this together without any prior experience with db-centric apps.
 

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
Not open for further replies.