SQLAlchemy vs Other ORMs
pythoncentral.io
pythoncentral.io
In statically typed languages, you can catch a lot more errors at compile time with ORM's than you can if you write queries by hand.
Good unit tests are critical regardless, it's just one more nice thing.
For anything simple, I vastly prefer ORMs. Joins and single selects are incredibly verbose and error-prone compared to "user.books" and "user.address" or the equivalent, and the added layer of abstraction means it's easier to modify the table without modifying code.
On the other hand, ORMs can be a huge source of inefficiency if you don't look at the generated SQL and use lazy collection thoughtlessly, though they avoid a lot of boilerplate when saving a complex object graph.
A happy(?) medium is probably something with a powerful data retrieval API, which makes for composable queries (SQL query strings compose very badly).
All these discussions of which is better (ORM or straight SQL) should be put in context of the type of application, the amount of data and the complexity of the schema.
I have worked with systems which constrain all logic to the application server and when the data set is small it works fine, when its not, its a nightmare. Why should you bring down a million rows down to your application layer just so you can update a couple of columns. Doing this in the database as a set operation becomes trivial and fast.
Can you explain? I have no problem and have used or written my own SQL composition libraries pretty easily. It is code-composition, no doubt, but the BNF for SQL is very easy to grok even given the variations in platform.
On the other hand, SQLAlchemy's query API is quite nice, and composes just fine.
On the flipside, how do you feel about slower performance? About diagnosing problems through multiple abstraction layers? Or about having different abstraction layers for different languages?
I don't really know programmers that don't occasionally have to make things go faster.
And most program or work in environments with multiple languages.
And anyone using an ORM ends up having to know both the ORM and SQL. And ends up having to diagnose problems across both.
So, nope.
However, sometimes it's much simpler to just write custom SQL.
I will say, though, that SQLAlchemy is very nice for an ORM.
My interpretation is that the relational and OO models are incompatible. Not 100% incompatible, but with enough dissimilarity to make writing a complete and efficient ORM a near impossible task.
Every ORM can easily do the simplest of mappings. Classes to tables, rows to instances is the very basic stuff. It is when you have complex data structures that the task of the ORM is close to impossible. Either you have a DSL for describing this mapping, or you fall back into the general language the rest of the program is written in.
I see little value in a DSL for describing relational to OO mappings, so I write the mapping in the general programming language already in use, hence I do not find any added value in ORMs.
I came here to read people's thoughts on the article but the top comment shifts the debate onto a wider topic.
This happens all the time on HN and is something I find slightly irritating if I'm actually interested in the topic itself. It's like that person at a dinner party who always changes the subject onto what THEY want to talk about.
I'm not blaming you - it's the fact that your post is the top one and the one most commented on that derails the original topic - which is a group decision.
Oh well. Democracy in action I suppose.
In my defence - the $other_thing is the actual article though.
It's also that there are some predictable topics. Every post about a new Google product has someone bringing up the Reader shutdown and every post about ORMs has someone talking about how much they prefer raw SQL...
I think your point of contention is that other people, reading this very same article, approve of a comment that did the exact same thing you did: made a point about a "meta"-topic by forking the conversation into some other concept that was assumed when both of you were compelled to comment. Now we have three (your parent, you, and me) off-topic forks of a conversation that is superfluous to the so-called ubiquitous topic of the thread.
I see where you are coming from, but I find these "meta"-topics at times more interesting than the topic itself, hence why I'm commenting.
Interests are interests, your response is just as much justified IMO as your parent commenter.
In those cases where you need to compose SQL, it can save you a ton of time with a SQL composition API (rather than building your own strings).
That is the basis of how SQL Alchemy builds up its ORM functionality. So if you are working on a project where you can save some time using the ORM, but prefer to work closer to SQL, you've got a great mix.
What's the problem with an ORM again?
Half the point of SQLA is that the ORM is built on the core SQL generation. It's the least magical and most RDBMS-friendly ORM I've ever seen. It's designed for people who already grok SQL, not people who'd like to forget that it exists. You can even jam hand-written SQL into the ORM if you so please.
* https://docs.djangoproject.com/en/dev/ref/models/custom-look...
SQLAlchemy is more interesting in that it doesn't hide SQL away, just provides an API for it. Underneath everything you're still dealing with strings (you can just print most objects, like queries, columns, expressions, SQL functions, etc), so you're less likely to be suddenly incompatible with the rest of the API when doing something that deviates a little more from the common cases.
Are you sure you understood how to use the full functionality? What was missing (besides CTEs)?
for example, last week I wanted to a bulk INSERT. maybe arel can do this (though I highly suspect it cannot since this isn't a part of relational algebra at all), but that's kind of worthless if i can't find any evidence of how to do it without reading the arel source.
Are there many other Python developers working directly with libpqxx though? I've recently started exploring the idea of moving some of my relatively stable, postgres-specific code into a dedicated library and would be grateful for tips or words of caution. My hope is that this approach will wind up being useful in cases where I'm coding against a large body of pre-existing stored procedures.
Am I the only person that finds this preferable to coupling your objects with the ORM?
I believe this is a better way to approach the mismatch between the object model and the relational model.
However I don't have the resources to maintain this very old project right now (it's only about 500 lines, anyone can pick up the source if they cared).
A more SQLA-centric version of this idea is recently released as the automap extension (http://docs.sqlalchemy.org/en/latest/orm/extensions/automap....) which includes the "map everything on the fly" step plus relationship support, and you then use traditional Query patterns with it. Reaction to it has been mixed, depending on where the user is coming from.