gpt4 book ai didi

python - 将 connection_factory 指定给 SQLAlchemy 的 create_engine()

转载 作者:行者123 更新时间:2023-11-29 14:23:12 24 4
gpt4 key购买 nike

我有一个自定义连接工厂类(继承自 psycopg2.extensions.connection),我希望 SQLAlchemy 使用它。从 create_engine() 文档中,

**kwargs takes a wide variety of options which are routed towards their appropriate components. Arguments may be specific to the Engine, the underlying Dialect, as well as the Pool. Specific dialects also accept keyword arguments that are unique to that dialect.

当我尝试指定一个 connection_factory 参数时,如下所示:

engine = create_engine(dsn.engine_info(), connection_factory=ConnectionEx)

我得到这个回溯:

Traceback (most recent call last):   File "foo.py", line 8, in <module>
from user import test_user File "/vagrant/workspace/panel/panel/user.py", line 18, in <module>
from panel.helpers import intval File "/vagrant/workspace/panel/panel/__init__.py", line 51, in <module>
import panel.views File "/vagrant/workspace/panel/panel/views.py", line 13, in <module>
from panel.api import api_functions File "/vagrant/workspace/panel/panel/api/api_functions.py", line 27, in <module>
from panel.targeting import SavedTargetSet File "/vagrant/workspace/panel/panel/targeting.py", line 19, in <module>
from panel.database import panelists_tbl, us_cities_tbl, income_buckets_tbl File "/vagrant/workspace/panel/panel/database.py", line 39, in <module>
engine = create_engine(dsn.engine_info(), connection_factory=ConnectionEx) File "/home/vagrant/.virtualenvs/project/lib/python2.6/site-packages/sqlalchemy/engine/__init__.py", line 331, in create_engine
return strategy.create(*args, **kwargs) File "/home/vagrant/.virtualenvs/project/lib/python2.6/site-packages/sqlalchemy/engine/strategies.py", line 141, in create
engineclass.__name__)) TypeError: Invalid argument(s) 'connection_factory' sent to create_engine(), using configuration PGDialect_psycopg2/QueuePool/Engine. Please check that the keyword arguments are appropriate for this combination of components.

最佳答案

当文档谈论“适当的组件”时,它指的是 SQLAlchemy API 的组件,而不是各种驱动程序。由于 connection_factory 是需要发送给 connect() 的参数,因此您应该在调用 create_engine 时使用关键字 connect_args ( documentation 也提到了 here )。因此:

engine = create_engine(
dsn.engine_info(), connect_args={'connection_factory': ConnectionEx})

关于python - 将 connection_factory 指定给 SQLAlchemy 的 create_engine(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14265796/

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