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 Introduction to SQLAlchemy Querying the Movie Database

Anthony Piscocama
Anthony Piscocama
5,704 Points

The movie theater has added all of their current offerings to the database you created, but they need some help finding

i am not sure where i am going wrong.

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)

# Write your code below
for movie in session.query(Movie).filter_by(genre=='Romance'):
    romance_movies = movie

2 Answers

Megan Amendola
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree seal-36
Megan Amendola
Treehouse Teacher

Hi! The query you have already holds all of the romance movies, so you just need to set it equal to the variable name.

romance_movies = session.query(Movie).filter_by(genre=='Romance')

Right now your for loop is overriding the variable with a new movie each time. For example, if the movies it returned were 'The Notebook', 'Romancing the Stone', and 'Titanic'. The loop will grab the first movie, 'The Notebook' and set it equal to the variable, then it will grab the next one, 'Romancing the Stone', and set the variable to now equal this movie, and finally, it will grab the last movie, 'Titanic', and set the variable equal to this last movie. So at the end of the loop, the variable equals the final movie, 'Titanic', instead of the whole list of movies.

new_movie = Movie(movie_title='Titanic', genre='Romance') session.add(new_movie) session.commit() session.delete(new_movie)