I have a custom connection factory class (which inherits from psycopg2.extensions.connection) that I would like SQLAlchemy to use. From the create_engine() documentation,
**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.
When I try to specify a connection_factory parameter, like this:
engine = create_engine(dsn.engine_info(), connection_factory=ConnectionEx)
I get this traceback:
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.
When the documentation is talking about “appropriate components,” its referring to the components of the SQLAlchemy API, rather than the various drivers. Since
connection_factoryis a parameter that needs to be sent toconnect(), you should use the keywordconnect_argsin your call tocreate_engine(documentation also mentioned here). Thus: