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 Movie Database Continued

Jonathan Grieve
MOD
Jonathan Grieve
Treehouse Moderator 91,253 Points

Add a new movie instance to the database

Now, what could I be doing wrong here? It looks to me like I've correctly added a movie instance. It's asking me to create a variable called new_movie which I've done. And assign a new instance.

But I only get the following.

Bummer: You should have a variable called new_movie that creates an instance of Movie().    

The only thing I can think of is an issue with indentation.

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)
Base = declarative_base()


class Movie(Base):
    __tablename__ = β€˜movies’

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

    Session = sessionmaker(bind=engine)
    session = Session()


    new_movie = Movie(movie_title="Robin Hood: Prince of Thieves", genre="Action")
    session.add(new_movie)

1 Answer

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there, Jonathan Grieve! Your final two lines are defined inside the class. They should be moved over to the left so that they line up with the class keyword. You want to make a new instance of Movie from outside the class.

Hope this helps! :sparkles:

Chris Freeman
Chris Freeman
Treehouse Moderator 68,454 Points

The actual issue is that the feeble regex used to evaluate the code does not like the colon : in the title. 🀦

if Jonathan Grieve, left his session statements inside the class, and even commented them out, the regex would still "pass" the code.

Jonathan Grieve
Jonathan Grieve
Treehouse Moderator 91,253 Points

Hi there, yes I thought it might be something like this, but I had tried leaving those last 2 lines to line up in the global scope. Maybe affected by the issue Chris Freeman mentioned. πŸ˜ƒ

Chris Freeman
Chris Freeman
Treehouse Moderator 68,454 Points

This "passes" because only regex used to evaluate. It should fail due to session not defined outside the class. Code in comments will still "pass". Note: colon had to be removed to pass regex.

class Movie(Base):
    __tablename__ = 'movies'
    id = Column(Integer, primary_key=True)
    movie_title = Column(String)
    genre = Column(String)
    # Session set up in the wrong place!
    Session = sessionmaker(bind=engine)
    session = Session()
new_movie = Movie(movie_title="Robin Hood Prince of Thieves", genre="Action")
session.add(new_movie)
# session.commit()
# comment it out and it still passes
# session.delete(new_movie)

The least they could do is run the code through a true parser to see if it compiles.

Chris Freeman
Chris Freeman
Treehouse Moderator 68,454 Points

to be overly precise, and total ...ectomy, because a ; semi would still fail.