2

I've come into the following error:

sqlalchemy.exc.OperationalError
OperationalError: (OperationalError) no such table: user u'SELECT user.id AS user_id, user.name AS user_name, user.password AS user_password \nFROM user \nWHERE user.id = ?' (1,)

I'm not sure how to debug this but I think it must be coming because its not loading the db file that I already generated using my models.py file. I've loaded that db file and made sure that the users table exists, and it does with data, but I can't figure out how to connect my flask application to the database.

Here's my models.py that I ran beforehand to generate the tables (I haven't included the declarations above it):

from datetime import datetime
import os
from sqlalchemy import Column, ForeignKey
from sqlalchemy import Boolean, DateTime, Integer, String, Text
from sqlalchemy.orm import relationship, synonym, backref

from sqlalchemy.ext.declarative import declarative_base

Base = declarative_base()


""" User """
class User(Base):
    __tablename__ = 'user'
    id = Column(Integer, primary_key=True)
    name = Column(String(200))
    password = Column(String(100))

    def __init__(self, name, password):
        self.name = name
        self.password = password

    def __repr__(self):
        return '<User %r>' % self.name
if __name__ == '__main__':
    from datetime import timedelta

    from sqlalchemy import create_engine
    from sqlalchemy.orm import sessionmaker

    PWD = os.path.abspath(os.curdir)

    engine = create_engine('sqlite:///{}/arkaios.db'.format(PWD), echo=True)

    Base.metadata.create_all(engine)
    Session = sessionmaker(bind=engine)
    session = Session()

    # Add a sample user
    user = User(name='Philip House', password="test")
    session.add(user)
    session.commit()

Here's app.py:

from flask import Flask
from flask.ext.sqlalchemy import SQLAlchemy
from flask import render_template

from arkaios.models import Base, User
from arkaios import config

app = Flask(__name__)
app.config.from_object(config)

db = SQLAlchemy(app)
db.Model = Base

@app.route('/admin/large-group')
def largeGroupOverview():
    user = db.session.query(User).filter_by(id=1)
    return render_template('largegroup/overview.html', user=user)

@app.route('/admin/large-group/<int:event_id>')
def largeGroupAttendance(event_id):
    return render_template('largegroup/attendance.html')

@app.route('/focus')
def largegroup():
    return 'Focus work'

And finally, app.py refers to config.py which is below:

import os
PWD = os.path.abspath(os.curdir)

DEBUG=True
SQLALCHEMY_DATABASE_URI = 'sqlite:///{}/arkaios.db'.format(PWD)
SECRET_KEY = 'thisissecret'
SESSION_PROTECTION = 'strong'

I can post the stack trace as well if need be! There must be something missing conceptually about how I'm thinking about how to connect Flask and SQLAlchemy, but I can't figure it out.

Thanks for any help :)

phouse512
  • 650
  • 4
  • 15
  • 27

1 Answers1

0

Are you running python models.py before python app.py app to create the database? The if __name__ == "__main__" will keep that part of the code from running when you import the file from app.py.

I would also make sure the db is valid using sqlite3 and run the .tables command.

Kyle James Walker
  • 1,238
  • 14
  • 16