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 Analyzing the Movie Database

Andy McDonald
Andy McDonald
5,016 Points

How to use like() method?

The quiz is expecting me to know how to use this when this is the first time I've ever seen anything like it. I can't google search for anything that is helping with this particular method. A hint on how to use this to get passed the quiz would be helpful

models.py
from sqlalchemy import create_engine, Column, Integer, String
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker


engine = create_engine(sqlite:///movies.db, echo=False)
Session = sessionmaker(bind=engine)
session = Session()
Base = declarative_base()


class Movie(Base):
    __tablename__ = movies

    id = Column(Integer, primary_key=True)
    movie_title = Column(String)
    genre = Column(String)

def the_movies():
    return session.query(Movie).filter(Movie.movie_title.like('%The%').count()
Andy McDonald
Andy McDonald
5,016 Points

I did use it as a string and would never have known that it behaved completely differently with a query object had you not hinted to use it without any argument. I originally was just guessing what the quiz wanted but when I went back to look at my meticulous notes I started trying to pass arguments into it.

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 67,763 Points

Hey Andy McDonald, had to look a bit too! You are very close.

  • The like() is defined in the docs here

  • usage examples can be found in the docs on this page

The challenge asks to create a variable named the_movies. You've defined a function. So use, the_movies = ...

  • there is a syntax error where you are missing a closing paren before .count()

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

Andy McDonald
Andy McDonald
5,016 Points

I was messing with count() in workspaces and it insisted on passing an argument. Why did the quiz allow me to use it with no argument?

Chris Freeman
Chris Freeman
Treehouse Moderator 67,763 Points

Can you show the code that caused the request for an argument?

Many objects may have their own method named count(). The query set count() does not take an argument. list.count() takes exactly one argument, str.count() takes one or more. Perhaps the object requiring arguments wasn’t a query set.