FlaskFM project step 13 import db error

I’m at step 13 of the project FlaskFM and when I try to import the database db from app.py on the python terminal it shows an ImportError on line 2 because it cannot import name ‘SQLALchemy’. My app.py file is as shown here:

from flask import Flask, render_template
from flask_sqlalchemy import SQLALchemy

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///song_library.db'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.config['SECRET_KEY'] = 'you-will-never-guess'
#create an SQLAlchemy object named `db` and bind it to your app
db = SQLALchemy(app)
#a simple initial greeting
@app.route('/')
@app.route('/index')
def greeting():
    return render_template('greeting.html')

# app name 
@app.errorhandler(404) 
def not_found(e): 
  return render_template("404.html") 

#uncomment the code below here when you are done creating database instance db and models
import routes

And the models.py file is this:

from app import app, db

#the User model: each user has a username, and a playlist_id foreign key referring
#to the user's Playlist
class User(db.Model):
  id = db.Column(db.Integer, primary_key = True)
  username = db.Column(db.String(50), index = True, unique = True) 
  playlist_id = db.Column(db.Integer,  db.ForeignKey('playlist.id'))
  
  #representation method
  def __repr__(self):
        return "{}".format(self.username)

#create the Song model here + add a nice representation method
class Song(db.Model):
  id = db.Column(db.Integer, primary_key = True)
  artist = db.Column(db.String(50), index = True, unique = False)
  title = db.Column(db.String(50), index = True, unique = False)
  n = db.Column(db.Integer, index = False, unique = False)
  def __repr__(self):
      return '{} by {}'.format(self.title, self.artist)
#create the Item model here + add a nice representation method
class Item(self):
  id = db.Column(db.Integer, primary_key = True)
  song_id = db.Column(db.Integer, db.ForeignKey('song.id'))
  playlist_id = db.Column(db.Integer, db.ForeignKey('playlist.id'))
#create the Playlist model here + add a nice representation method
class Playlist(self):
  id = db.Column(db.Integer, primary_key = True)
  items = db.relationship('Item', backref = 'playlist', lazy = 'dynamic')

Hi!

You have a typo: SQLALchemy instead of SQLAlchemy (with a small l). Python is case sensitive.

1 Like

Hi! Thank you I definitely had that typo. Also had another error in the argument of the classes Playlist and Item because I should have defined them as models.

Thank you for the help!