1

I have a Flask application with SQLAlchemy that was working fine until now. Suddenly, while the instance folder is being created, the database file isn't. I tried db.create_all() via shell, it works fine.

Error:

OperationalError

sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) no such table: user
\[SQL: SELECT user.id AS user_id, user.username AS user_username, user.name AS user_name, user.email AS user_email, user.password AS user_password
FROM user
WHERE user.email = ?
LIMIT ? OFFSET ?\]
\[parameters: ('[email protected]', 1, 0)\]

Project Structure:

v1/
    app/
        __init__.py
        models.py
        routes.py
        instance/    # Created but empty
    config.py
    run.py

My code:

app/__init__.py:

from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from config import DevelopmentConfig

app = Flask(__name__)
app.config.from_object(DevelopmentConfig)
db = SQLAlchemy(app)

app.app_context().push()

from app import routes
from app.models import User

config.py:

class Config:
    SQLALCHEMY_DATABASE_URI = 'sqlite:///quizmaster.db'
    SQLALCHEMY_TRACK_MODIFICATIONS = False

class DevelopmentConfig(Config):
    DEBUG = True
    SECRET_KEY = 'super-secret-key'

models.py:

from app import db

class User(db.Model):
    __tablename__ = 'user'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    username = db.Column(db.String(15), unique=True, nullable=False)
    name = db.Column(db.String(80), nullable=False)
    email = db.Column(db.String(120), unique=True, nullable=False)
    password = db.Column(db.String(80), nullable=False)

run.py:

from app import app, db
from app.models import User
from sqlalchemy import or_

if __name__ == '__main__':
    db.create_all()
    # Create admin user if not exists
    admin_exists = User.query.filter(
        or_(
            User.username == 'admin',
            User.email == '[email protected]'
        )
    ).first()
    if not admin_exists:
        admin = User(
            id=0,
            username='admin',
            name='admin',
            email='[email protected]',
            password='admin123'
        )
        db.session.add(admin)
        db.session.commit()

    app.run(debug=True)

What's strange is that this code was working fine until now. If i run this now, only the instance folder gets created, the database was not created. I tried deleting the instance folder and made it recreate, still same issue.

7
  • Please show the code of your db.py module where it creates the database. Commented Feb 25 at 12:05
  • @OldBoy I don't have db.py. I set up the db object in app/__init__.py, and the models in models.py. The db is created at run.py. Commented Feb 25 at 15:41
  • It looks like the call to db.create_all() is not initializing the database, but there is no indication as to why. Are you sure there should not be a call to db.init_app(app) before the create call? Commented Feb 25 at 16:05
  • I just followed this tutorial and it correctly created the database. I think you are missing some other setup code, as it looks like you call db.create_all() before the other initializations. Commented Feb 25 at 16:23
  • Thanks for the link. I had to remove if __name__ == '__main__' , since I run my app using flask run. My issue is resolved now. Commented Feb 26 at 18:07

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.