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 Search for a Book

S M Tasfiqul Asif
S M Tasfiqul Asif
11,514 Points

Getting AttributeError: 'Query' object has no attribute 'title https://github.com/Tasfiq23/library_database.git

I am getting an error AttributeError: 'Query' object has no attribute 'title. I have gone through the code twice but could not find out what's wrong with it.

github repo link: https://github.com/Tasfiq23/library_database.git

code:

id_options = []
            for book in session.query(Book):
                id_options.append(book.id)
            id_error= True
            while id_error:    
                id_choice=input(f'''
                    \nId Options: {id_options}
                    \rBook id: ''')
                id_choice = clean_id(id_choice,id_options)
                if type(id_choice) == int:
                    id_error=False
            the_book = session.query(Book).filter(Book.id==id_choice)
            print(f'''\n{the_book.title} by {the_book.author} \rPublished:{the_book.published_date} \rPrice: ${the_book.price/100}''')

[MOD: added ```python formatting -cf]

3 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,423 Points

Hey S M Tasfiqul Asif, a query that returns a single item is still a query object and not that item.

Earlier in the code, there is a loop over a query result for book in session.query(Book):. This works because a query object is an iterable. Using a filter still results in a query object even if exactly one item is found.

A quick check to test if this is the issue:

  • add [0] to the end of the query, so that the_book is assigned to the first result. This hack is not robust, because if the query object is empty it raises a IndexError. But is will show if this is the issue.
            the_book = session.query(Book).filter(Book.id==id_choice)[0]

Post back if you need more help. Good luck!!

S M Tasfiqul Asif
S M Tasfiqul Asif
11,514 Points

Hi Chris, The object I was hoping to get is from the below Book class, so I don't understand why I am getting the error saying query has no attribute as 'title'.

class Book(Base):
    __tablename__ = 'books'

    id = Column(Integer,primary_key= True)
    title = Column('Title',String)
    author = Column('Author',String)
    published_date = Column('Published',Date)
    price = Column('Price',Integer)

    def __repr__(self):
        return f'Title: {self.title} Author : {self.author} Published : {self.published_date} Price : {self.price},'

Also, I run the code by adding [0] to the end of the query and the output I am getting is

"Price: $29.997-10-25 Bader ".

Note: The first entry in my database is,

1|Python Tricks|Dan Bader|2017-10-25|2999

So that means it is only showing the price,date and author's last name, which is even more confusing to me. why it is not printed out with full details?

print(f'''\n{the_book.title} by {the_book.author} \rPublished:{the_book.published_date} \rPrice: ${the_book.price/100}''')
Chris Freeman
Chris Freeman
Treehouse Moderator 68,423 Points

It’s not obvious, but the f-string is using \r (return) instead of \n (newline) this causes characters to be overwritten.

S M Tasfiqul Asif
S M Tasfiqul Asif
11,514 Points

Hi Chris, i understand now and It works.Thank you so much. :D