More specifically, I see that instead of using the natural SQL-like join syntax in SQLAlchemy, a lot of people tend to use filters for purposes of joins. To elaborate here's how I would do a join:
(session.Query(Book)
.join(Author, Publisher, Retailer)
.filter(
Author.name == "Crenshaw Adams",
Publisher.country == "United States",
Retailer.is_online == True))
The join columns are implicit in the relationships as defined in the model declaration file.
But everywhere else (especially on StackOverflow) I've seen people do it as:
(session.Query(Book)
.filter(
Book.author_id == Author.id,
Author.publisher_id == Publisher.id,
Publisher.retailer_id == Retailer.id,
Author.name == "Crenshaw Adams",
Publisher.country == "United States",
Retailer.is_online == True
))
Which of these methods is the right way to do it? Which is more Pythonic? Or, at the very least, more idiomatic in the way SQLAlchemy is supposed to be used? And is there a difference in terms of DB resource usage, or local machine resource usage (that is, one is more stressful on CPU and RAM of DB and less on local machine, and vice versa)?
In addition, the former way doesn't allow for the update()
method on the Query API - it complains about multi-table updates not being allowed - even if I'm updating only a single table. The latter allows for update()
to work just fine.