SQLAlchemy & Elixir

Note: This assumes that you have Elixir installed and enabled in your web framework. Refer to your framework’s documentation for details on how to do this.

Define Model

Add the following to your model file. The actual file depends on the framework:

import elixir as el

class Movie(el.Entity):
    title = el.Field(el.String(100))
    description = el.Field(el.Text)
    year = el.Field(el.Integer)
    genera = el.Field(el.String(100))
    release_date = el.Field(el.Date)


Elixir automatically creates an integer primary key column, called id. Note: el.setup_all() may not be necessary, depending on your framework.

When you’ve done this, you need to tell your web framework to create the database tables, see the framework-specific instructions.

Load Data

In the movie controller method, replace the following lines:

# TBD: load movie from database
movie = None


movie = id is not None and db.Movie.get(id)
if id is not None and not movie:
    raise Exception('Movie ID not found')

Save Data

In the save_movie controller method, replace the line:

# TBD: save movie to database


if id is not None:
    movie = db.Movie.get(id)
    if not movie:
        raise Exception('Movie ID not found')
    movie = db.Movie()