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

Databases

For SQLAlchemy, it is asking me to create a movie and add movie to session. Unsure what I'm doing wrong

Grateful for any help! :) The question is: Now you can create a movie to add to your database. Use a variable named new_movie. Create a Movie() with whatever movie_title and genre you want. Then add your movie to session.

My code is:

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)


if __name__ == "__main__":
    Base.metadata.create_all(engine)

    new_movie = Movie(movie_title = "Elf", genre = "Christmas")
    session.add(new_movie)

My error is: Bummer: You should have a variable called new_movie that creates an instance of Movie(). Traceback (most recent call last): File "", line 16, in test_init_code AssertionError: Regex didn't match: 'new_movie\s*=\s*Movie\(\s*\r*\n*movie_title=\\'?\"?\’?[a-zA-Z0-9\s\-\\'\&\:\,\!]\\'?\"?\’?\s,\s*\r*\n*genre=\\'?\"?\’?[a-zA-Z\s\-\\']\\'?\"?\’?\s\r*\n*\)' not found in 'from sqlalchemy import create_engine, Column, Integer, String\nfrom sqlalchemy.ext.declarative import declarative_base\nfrom sqlalchemy.orm import sessionmaker\n\n\nengine = create_engine(‘sqlite:///movies.db’, echo=False)\nSession = sessionmaker(bind=engine)\nsession = Session()\nBase = declarative_base()\n\n\nclass Movie(Base):\n tablename = ‘movies’\n\n id = Column(Integer, primary_key=True)\n movie_title = Column(String)\n genre = Column(String)\n \nif name == "main":\n Base.metadata.create_all(engine)\n \n new_movie = Movie(movie_title = "Elf", genre = "Christmas")\n \n session.add(new_movie)' : You should have a variable called new_movie that creates an instance of Movie().

2 Answers

Jonathan Grieve
MOD
Jonathan Grieve
Treehouse Moderator 91,253 Points

It's been a while but I believe you need to add

session.commit()

to the end of your code to make sure that SQLAlchmey recognises that you want to ad da table or add a record to it. :)

I thought it might be that - it still doesn't work though. Very strange. Thanks for your help! :)

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 __repr__(self):
        return f'<User(movie_title={self.movie_title}, genre={self.genre})>'


if __name__ == "__main__":
    Base.metadata.create_all(engine)

    new_movie = Movie(movie_title = "Elf", genre = "Christmas")
    session.add(new_movie)
    session.commit()