sqlalchemy.exc.OperationalError: (OperationalError) no such table

See original GitHub issue

Hello Miguel, I’ve finished your book and tried to build a “notebook” project without the help of the book. “Notebook” is a simplified flasky, no User, no Role, no Migrate, No Manage, just Post.

I thought it would be easy, and everything went well until I added these two lines in views.py: posts = Post.query.order_by(Post.timestamp.desc()).all() return render_template('index.html', current_time = datetime.utcnow(), form = form, posts = posts) I got the following error: sqlalchemy.exc.OperationalError: (OperationalError) no such table

I read issue #18, but still can’t solve my problem(I didn’t use manager to modify the command line). Could you please tell me what’s wrong with it? Thank you !!

Here’s my code:

run.py

# -*- coding:utf-8 -*-
from app import create_app
app = create_app()
if  __name__ == '__main__':
     app.run(debug = True)

init.py

from flask import Flask
from flask_sqlalchemy import SQLAlchemy 
from flask_moment import Moment
from flask_bootstrap import Bootstrap


moment = Moment ()
bootstrap = Bootstrap()
db = SQLAlchemy()

def create_app():
    app = Flask(__name__)
    moment.init_app(app)
    bootstrap.init_app(app)
    app.secret_key = 'SECRET_KEY'
    db.init_app(app)
    with app.app_context():
           db.create_all()

    from .main import main as main_blueprint
    app.register_blueprint(main_blueprint)

    return app

views.py

from flask import render_template, redirect, url_for, session
from datetime import datetime
from . import main
from app.forms import PostForm
from flask_login import current_user
from ..models import Post


@main.route('/', methods = ['GET', 'POST'])
def index():
    form = PostForm()
    if form.validate_on_submit():
        post = Post(body = form.body.data)
        db.session.add(post)
        db.session.commit()
        return redirect(url_for('.index'))
    posts = Post.query.order_by(Post.timestamp.desc()).all()
    return render_template('index.html', current_time = datetime.utcnow(), form = form, posts = posts)

@main.errorhandler(404)
def page_not_found(e):
    return render_template('404.html'), 404

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:11 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
tiansiyuancommented, Jul 1, 2017

Without the following code, I got the same error.

with app.app_context(): db.create_all()

It is not in the original repository.

2reactions
miguelgrinbergcommented, Jul 7, 2016

The error is pretty clear, you have not created the database table. You have defined the model, but then a table that matches that model needs to be created in the database. If you are using migrations, then you will have migration that creates the table. If you are not using migrations, then you have to call db.create_all() to create your tables. This is covered near the beginning of the book, before migrations are introduced.

Read more comments on GitHub >

github_iconTop Results From Across the Web

(sqlite3.OperationalError) no such table - Stack Overflow
I just got done setting up a Flask app and I dealt with this kind of problem. I strongly suspect the problem here...
Read more >
(sqlite3.OperationalError) no such table: users · Issue #15 ...
I was wondering if you had some indicative code that runs the same framework on CQLalchemy, not SQLalchemy. My aim is to not...
Read more >
(sqlite3.OperationalError) no such table: books : r/flask - Reddit
If an error says "table does not exist" check first that you are connected to the correct database. Second, make sure the table...
Read more >
Trying to walk through tutorial, getting (OperationalError) no ...
Trying to walk through tutorial, getting (OperationalError) no such table. 2731 views ... from sqlalchemy.orm.exc import NoResultFound
Read more >
(sqlite3.OperationalError) no such table: user : Forums ...
Error: sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) no such table: user. Hi, I get this error, the table "user" exists in the ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found