我在我的应用程序中集成了Flask和SQLAlchemy,但可以理解,为什么在登录页面获取大堆栈跟踪。现在我从Debian切换到Mac OS后会出现这个错误。所有在virtuaelnv中使用的库。
我现在有这些东西:
1)在flask_app.py中:
from flask import Flask, request, session
from flask_sqlalchemy import SQLAlchemy
from db import tables as dbTables
from db.queries import engine as db_engine
app = Flask(__name__, template_folder='./web/templates/', static_folder='./web/static/', static_url_path='')
app.config['SECRET_KEY'] = 'some_secret_key'
app.config['SQLALCHEMY_DATABASE_URI'] = db_engine
db_connection = SQLAlchemy(app)
import web.admin
import web.views
if __name__ == "__main__":
from web.admin import *
from web.views import *
app.run(port=8500, debug=True)
2)我的queries.py,其中包含从PostgreSQL DB获取一些数据的字符串和代码:
import datetime
from hashlib import sha256
from time import gmtime, strftime
import sqlalchemy
import sqlalchemy.exc
from sqlalchemy import and_, func, asc
from sqlalchemy.orm import sessionmaker
from tables import File as FileTable
from tables import Users, FileServer, FileSpace, Catalog
engine = sqlalchemy.create_engine("postgresql://dev:developer@localhost/csan", pool_size=20, max_overflow=0)
Session = sessionmaker(bind=engine)
# and some queries for DB
# ...
3)此堆栈跟踪在发送到http://localhost:8500/admin/login
POST请求后发送:
AttributeError
AttributeError: 'Engine' object has no attribute 'drivername'
Traceback (most recent call last)
File "/Users/savicvalera/code/Helenae/lib/python2.7/site-packages/flask/app.py", line 1836, in __call__
return self.wsgi_app(environ, start_response)
File "/Users/savicvalera/code/Helenae/lib/python2.7/site-packages/flask/app.py", line 1820, in wsgi_app
response = self.make_response(self.handle_exception(e))
File "/Users/savicvalera/code/Helenae/lib/python2.7/site-packages/flask/app.py", line 1403, in handle_exception
reraise(exc_type, exc_value, tb)
File "/Users/savicvalera/code/Helenae/lib/python2.7/site-packages/flask/app.py", line 1817, in wsgi_app
response = self.full_dispatch_request()
File "/Users/savicvalera/code/Helenae/lib/python2.7/site-packages/flask/app.py", line 1477, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/Users/savicvalera/code/Helenae/lib/python2.7/site-packages/flask/app.py", line 1381, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/Users/savicvalera/code/Helenae/lib/python2.7/site-packages/flask/app.py", line 1475, in full_dispatch_request
rv = self.dispatch_request()
File "/Users/savicvalera/code/Helenae/lib/python2.7/site-packages/flask/app.py", line 1461, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/Users/savicvalera/code/Helenae/lib/python2.7/site-packages/flask_admin/base.py", line 62, in inner
return f(self, *args, **kwargs)
File "/Users/savicvalera/code/Helenae/Helenae/helenae/web/admin.py", line 42, in login_view
if helpers.validate_form_on_submit(form):
File "/Users/savicvalera/code/Helenae/lib/python2.7/site-packages/flask_admin/helpers.py", line 47, in validate_form_on_submit
return is_form_submitted() and form.validate()
File "/Users/savicvalera/code/Helenae/lib/python2.7/site-packages/wtforms/form.py", line 310, in validate
return super(Form, self).validate(extra)
File "/Users/savicvalera/code/Helenae/lib/python2.7/site-packages/wtforms/form.py", line 152, in validate
if not field.validate(self, extra):
File "/Users/savicvalera/code/Helenae/lib/python2.7/site-packages/wtforms/fields/core.py", line 200, in validate
stop_validation = self._run_validation_chain(form, chain)
File "/Users/savicvalera/code/Helenae/lib/python2.7/site-packages/wtforms/fields/core.py", line 220, in _run_validation_chain
validator(form, self)
File "/Users/savicvalera/code/Helenae/Helenae/helenae/web/forms.py", line 19, in validate_login
user = get_user(self.login.data)
File "/Users/savicvalera/code/Helenae/Helenae/helenae/web/forms.py", line 11, in get_user
return db_connection.session.query(dbTables.Users).filter_by(name=user).first()
File "/Users/savicvalera/code/Helenae/lib/python2.7/site-packages/sqlalchemy/orm/scoping.py", line 150, in do
return getattr(self.registry(), name)(*args, **kwargs)
File "/Users/savicvalera/code/Helenae/lib/python2.7/site-packages/sqlalchemy/util/_collections.py", line 910, in __call__
return self.registry.setdefault(key, self.createfunc())
File "/Users/savicvalera/code/Helenae/lib/python2.7/site-packages/flask_sqlalchemy/__init__.py", line 139, in __init__
bind=db.engine,
File "/Users/savicvalera/code/Helenae/lib/python2.7/site-packages/flask_sqlalchemy/__init__.py", line 780, in engine
return self.get_engine(self.get_app())
File "/Users/savicvalera/code/Helenae/lib/python2.7/site-packages/flask_sqlalchemy/__init__.py", line 797, in get_engine
return connector.get_engine()
File "/Users/savicvalera/code/Helenae/lib/python2.7/site-packages/flask_sqlalchemy/__init__.py", line 470, in get_engine
self._sa.apply_driver_hacks(self._app, info, options)
File "/Users/savicvalera/code/Helenae/lib/python2.7/site-packages/flask_sqlalchemy/__init__.py", line 739, in apply_driver_hacks
if info.drivername.startswith('mysql'):
AttributeError: 'Engine' object has no attribute 'drivername'
答案 0 :(得分:2)
除了Flask-SQLAlchemy已为您生成的第二个会话外,您正在创建第二个会话,并且您正在干扰Flask-SQLAlchemy。
导入db_connection
并使用db_connection.session
而不是创建新会话。