Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python SQLAlchemy Basics Working with SQLAlchemy Analyze Books

Andy McDonald
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Andy McDonald
Python Development Techdegree Graduate 13,801 Points

Why do I query the class name?

It doesn't fully make sense to me why I am querying the class name, not the table, or the DB its saved to. It makes more sense to me to query one of the latter. I get that its linked to the DB with the declarative base but in my mind its like querying 'item' in 'list', I guess that's just the way it is? Is there something that slipped passed me that would help me conceptualize this better?

https://teamtreehouse.com/library/analyze-books

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,457 Points

Quick answer: the session is the link to the database and Book allows reference to the class attribute Book.__tablename__ that has the value “books”.

  • So the query is run on the “books” table.
  • The order_by returns the ordered list of books (like a list), then
  • first() selects the first item (like books[0]).