将connection_factory指定为SQLAlchemy的create_engine()

时间:2013-01-10 19:30:46

标签: python postgresql sqlalchemy psycopg2

我有一个自定义连接工厂类(继承自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.

1 个答案:

答案 0 :(得分:2)

当文档讨论“适当的组件”时,它指的是SQLAlchemy API的组件,而不是各种驱动程序。由于connection_factory是需要发送至connect()的参数,因此您应在connect_args的调用中使用关键字create_enginedocumentation也提到engine = create_engine( dsn.engine_info(), connect_args={'connection_factory': ConnectionEx}) 3}})。因此:

{{1}}