gpt4 book ai didi

python - “引擎”对象没有属性 'drivername'

转载 作者:行者123 更新时间:2023-12-01 04:56:11 25 4
gpt4 key购买 nike

我将 Flask 和 SQLAlchemy 集成到我的应用程序中,但可以理解为什么在登录页面获取大堆栈跟踪。从 Debian 切换到 Mac 操作系统后,我现在会遇到此错误。 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)它是我的querys.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'

最佳答案

除了 Flask-SQLAlchemy 已经为您生成的 session 之外,您还正在创建第二个 session ,并且您正在干扰 Flask-SQLAlchemy。

导入db_connection并使用db_connection.session而不是创建新 session 。

关于python - “引擎”对象没有属性 'drivername',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27335251/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com