1

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

**kwargs 采用多种选项,这些选项被路由到相应的组件。参数可能特定于引擎、底层方言以及池。特定方言也接受该方言独有的关键字参数。

当我尝试指定 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.
4

1 回答 1

4

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

engine = create_engine(
    dsn.engine_info(), connect_args={'connection_factory': ConnectionEx})
于 2013-01-11T11:13:27.050 回答