Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python SQLAlchemy Basics Working with SQLAlchemy Analyze Books

Andy McDonald
Andy McDonald
5,016 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?


Chris Freeman
Chris Freeman
Treehouse Moderator 67,763 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]).