Smizz
Smizz

Reputation: 37

Table isn't available even though Flask-SQLAlchemy create_all was called

I'm trying to fetch records from my database, but I get

OperationalError: (sqlite3.OperationalError) no such table: driver

even though I defined the model and called db.create_all(). Why doesn't this work?

from flask import Flask
from flask_sqlalchemy import SQLAlchemy

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:////tmp/test.db'
db = SQLAlchemy(app)
db.create_all()

class Driver(db.Model):
    id = db.Column(db.Integer, primary_key=True)

@app.route('/')
def index():
    result = Driver.query.all()
    return render_template('index.html', result=result)

Upvotes: 1

Views: 67

Answers (1)

davidism
davidism

Reputation: 127180

You're calling create_all before defining your model. Move create_all to after all your models are defined or imported.

class Driver(db.Model):
    ...

db.create_all()

Upvotes: 2

Related Questions