我遇到了以下错误:
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,)
我不确定如何调试它,但我认为它一定会出现,因为它没有加载我已经使用我的 models.py 文件生成的 db 文件。我已经加载了那个 db 文件并确保 users 表存在,并且它与数据有关,但我不知道如何将我的烧瓶应用程序连接到数据库。
这是我预先运行以生成表格的 models.py(我没有包含上面的声明):
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()
这是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'
最后,app.py 指的是下面的 config.py:
import os
PWD = os.path.abspath(os.curdir)
DEBUG=True
SQLALCHEMY_DATABASE_URI = 'sqlite:///{}/arkaios.db'.format(PWD)
SECRET_KEY = 'thisissecret'
SESSION_PROTECTION = 'strong'
如果需要,我也可以发布堆栈跟踪!关于我如何考虑如何连接 Flask 和 SQLAlchemy,在概念上一定缺少一些东西,但我无法弄清楚。
谢谢你的帮助 :)