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 trialJassim Alhatem
20,883 PointsSomething wrong with sqlalchemy querying and filtering question?
I don't know what I'm doing wrong. I watched the video twice, read the docs and I'm still stuck. I'm stuck on task 1.
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)
# Write your code below
romance_movies = session.query(Movie).filter_by(Movie.genre=="romance")
for movie in romance_movies:
print(movie)
1 Answer
Chris Freeman
Treehouse Moderator 68,454 PointsYou are very close. The challenge checker wants genre=
instead of Movie.genre==
, and "Romance"
instead of "romance"
Note the difference in the cases below (from the docs). One uses filter
, the other uses filter_by
:
# filtering results, which is accomplished either with filter_by(), which uses keyword arguments:
sql
>>> for name, in session.query(User.name).\
... filter_by(fullname='Ed Jones'):
... print(name)
ed
# …or filter(), which uses more flexible SQL expression language constructs.
# These allow you to use regular Python operators with the class-level attributes on your mapped class:
sql
>>> for name, in session.query(User.name).\
... filter(User.fullname=='Ed Jones'):
... print(name)
ed
Check back if you need more help. Good luck!!
Jassim Alhatem
20,883 PointsThank you so much, Chris, you're a lifesaver. I thought filter and fliter_by were the same because when the challenge returned an "error" the message said "use filter or filter_by" and I understood it as either of them.
Chris Freeman
Treehouse Moderator 68,454 PointsChris Freeman
Treehouse Moderator 68,454 PointsWhat I don't know is why the checker allows both
filter
andfilter_by
as found in the regexfilter[_by]*
, but does not allow theMovie.genre
format allowed when usingfilter
. Flagging for feedback from developers.