Undocumented
Function | create_engine |
Create a new _engine.Engine instance. |
Function | engine_from_config |
Create a new Engine instance using a configuration dictionary. |
Create a new _engine.Engine
instance.
The standard calling form is to send the :ref:`URL <database_urls>` as the first positional argument, usually a string that indicates database dialect and connection arguments:
engine = create_engine("postgresql://scott:tiger@localhost/test")
Note
Please review :ref:`database_urls` for general guidelines in composing URL strings. In particular, special characters, such as those often part of passwords, must be URL encoded to be properly parsed.
Additional keyword arguments may then follow it which
establish various options on the resulting _engine.Engine
and its underlying .Dialect
and _pool.Pool
constructs:
engine = create_engine("mysql://scott:tiger@hostname/dbname", encoding='latin1', echo=True)
The string form of the URL is
dialect[+driver]://user:password@host/dbname[?key=value..], where
dialect is a database name such as mysql, oracle,
postgresql, etc., and driver the name of a DBAPI, such as
psycopg2, pyodbc, cx_oracle, etc. Alternatively,
the URL can be an instance of ~sqlalchemy.engine.url.URL
.
**kwargs takes a wide variety of options which are routed
towards their appropriate components. Arguments may be specific to
the _engine.Engine
, the underlying .Dialect
,
as well as the
_pool.Pool
. Specific dialects also accept keyword arguments that
are unique to that dialect. Here, we describe the parameters
that are common to most _sa.create_engine()
usage.
Once established, the newly resulting _engine.Engine
will
request a connection from the underlying _pool.Pool
once
_engine.Engine.connect
is called, or a method which depends on it
such as _engine.Engine.execute
is invoked. The
_pool.Pool
in turn
will establish the first actual DBAPI connection when this request
is received. The _sa.create_engine
call itself does not
establish any actual DBAPI connections directly.
Parameters | |
url | Undocumented |
**kwargs | Undocumented |
case_sensitive | if False, result column names will match in a case-insensitive fashion, that is, row['SomeColumn']. |
connect_args | a dictionary of options which will be passed directly to the DBAPI's connect() method as additional keyword arguments. See the example at :ref:`custom_dbapi_args`. |
convert_unicode=False | if set to True, causes
all
Deprecated since version 1.3: The :paramref:`_sa.create_engine.convert_unicode` parameter
is deprecated and will be removed in a future release.
All modern DBAPIs now support Python Unicode directly and this
parameter is unnecessary.
|
creator | a callable which returns a DBAPI connection. This creation function will be passed to the underlying connection pool and will be used to create all new database connections. Usage of this function causes connection parameters specified in the URL argument to be bypassed. This hook is not as flexible as the newer
See Also
|
echo=False | if True, the Engine will log all statements as well as a repr() of their parameter lists to the default log handler, which defaults to sys.stdout for output. If set to the string "debug", result rows will be printed to the standard output as well. The echo attribute of Engine can be modified at any time to turn logging on and off; direct control of logging is also available using the standard Python logging module. See Also :ref:`dbengine_logging` - further detail on how to configure logging. |
echo_pool=False | if True, the connection pool will log informational output such as when connections are invalidated as well as when connections are recycled to the default log handler, which defaults to sys.stdout for output. If set to the string "debug", the logging will include pool checkouts and checkins. Direct control of logging is also available using the standard Python logging module. See Also :ref:`dbengine_logging` - further detail on how to configure logging. |
empty_in_strategy | No longer used; SQLAlchemy now uses "empty set" behavior for IN in all cases. |
enable_from_linting | defaults to True. Will emit a warning if a given SELECT statement is found to have un-linked FROM elements which would cause a cartesian product.
New in version 1.4.
See Also |
encoding | legacy Python 2 value only, where it only applies to specific DBAPIs, not used in Python 3 for any modern DBAPI driver. Please refer to individual dialect documentation for client encoding behaviors. Defaults to the string value utf-8. This value refers only to the character encoding that is used when SQLAlchemy sends or receives data from a :term:`DBAPI` that does not support Python Unicode and is only used under Python 2, only for certain DBAPI drivers, and only in certain circumstances. Python 3 users please DISREGARD this parameter and refer to the documentation for the specific dialect in use in order to configure character encoding behavior. Note The encoding parameter deals only with in-Python encoding issues that were prevalent with some DBAPIS only under Python 2 only. Under Python 3 it is not used by any modern dialect. For DBAPIs that require client encoding configurations, which are most of those outside of SQLite, please consult specific :ref:`dialect documentation <dialect_toplevel>` for details. All modern DBAPIs that work in Python 3 necessarily feature direct support for Python unicode strings. Under Python 2, this was not always the case. For those scenarios where the DBAPI is detected as not supporting a Python unicode object under Python 2, this encoding is used to determine the source/destination encoding. It is not used for those cases where the DBAPI handles unicode directly. To properly configure a system to accommodate Python unicode objects, the DBAPI should be configured to handle unicode to the greatest degree as is appropriate - see the notes on unicode pertaining to the specific target database in use at :ref:`dialect_toplevel`. Areas where string encoding may need to be accommodated outside of the DBAPI, nearly always under Python 2 only, include zero or more of:
When using Python 3, the DBAPI is required to support all of the above values as Python unicode objects, which in Python 3 are just known as str. In Python 2, the DBAPI does not specify unicode behavior at all, so SQLAlchemy must make decisions for each of the above values on a per-DBAPI basis - implementations are completely inconsistent in their behavior. |
execution_options | Dictionary execution options which will
be applied to all connections. See
~sqlalchemy.engine.Connection.execution_options |
future | Use the 2.0 style
New in version 1.4.
See Also |
hide_parameters | Boolean, when set to True, SQL statement parameters
will not be displayed in INFO logging nor will they be formatted into
the string representation of
New in version 1.3.8.
See Also :ref:`dbengine_logging` - further detail on how to configure logging. |
implicit_returning=True | Legacy flag that when set to False will disable the use of RETURNING on supporting backends where it would normally be used to fetch newly generated primary key values for single-row INSERT statements that do not otherwise specify a RETURNING clause. This behavior applies primarily to the PostgreSQL, Oracle, SQL Server backends. Warning this flag originally allowed the "implicit returning" feature to be enabled back when it was very new and there was not well-established database support. In modern SQLAlchemy, this flag should always be set to True. Some SQLAlchemy features will fail to function properly if this flag is set to False. |
isolation_level | this string parameter is interpreted by various dialects in order to affect the transaction isolation level of the database connection. The parameter essentially accepts some subset of these string arguments: "SERIALIZABLE", "REPEATABLE READ", "READ COMMITTED", "READ UNCOMMITTED" and "AUTOCOMMIT". Behavior here varies per backend, and individual dialects should be consulted directly. Note that the isolation level can also be set on a
per- See Also
:paramref:`.Connection.execution_options.isolation_level`
- set per :ref:`SQLite Transaction Isolation <sqlite_isolation_level>` :ref:`PostgreSQL Transaction Isolation <postgresql_isolation_level>` :ref:`MySQL Transaction Isolation <mysql_isolation_level>` :ref:`session_transaction_isolation` - for the ORM |
json_deserializer | for dialects that support the
Changed in version 1.3.7: The SQLite dialect renamed this from
_json_deserializer.
|
json_serializer | for dialects that support the
Changed in version 1.3.7: The SQLite dialect renamed this from
_json_serializer.
|
label_length=None | optional integer value which limits the size of dynamically generated column labels to that many characters. If less than 6, labels are generated as "_(counter)". If None, the value of dialect.max_identifier_length, which may be affected via the :paramref:`_sa.create_engine.max_identifier_length` parameter, is used instead. The value of :paramref:`_sa.create_engine.label_length` may not be larger than that of :paramref:`_sa.create_engine.max_identfier_length`. |
listeners | A list of one or more
~sqlalchemy.interfaces.PoolListener objects which will
receive connection pool events. |
logging_name | String identifier which will be used within the "name" field of logging records generated within the "sqlalchemy.engine" logger. Defaults to a hexstring of the object's id. See Also :ref:`dbengine_logging` - further detail on how to configure logging. :paramref:`_engine.Connection.execution_options.logging_token` |
max_identifier_length | integer; override the max_identifier_length determined by the dialect. if None or zero, has no effect. This is the database's configured maximum number of characters that may be used in a SQL identifier such as a table name, column name, or label name. All dialects determine this value automatically, however in the case of a new database version for which this value has changed but SQLAlchemy's dialect has not been adjusted, the value may be passed here.
New in version 1.3.9.
|
max_overflow=10 | the number of connections to allow in
connection pool "overflow", that is connections that can be
opened above and beyond the pool_size setting, which defaults
to five. this is only used with ~sqlalchemy.pool.QueuePool . |
module=None | reference to a Python module object (the module
itself, not its string name). Specifies an alternate DBAPI module to
be used by the engine's dialect. Each sub-dialect references a
specific DBAPI which will be imported before first connect. This
parameter causes the import to be bypassed, and the given module to
be used instead. Can be used for testing of DBAPIs as well as to
inject "mock" DBAPI implementations into the _engine.Engine . |
paramstyle=None | The paramstyle to use when rendering bound parameters. This style defaults to the one recommended by the DBAPI itself, which is retrieved from the .paramstyle attribute of the DBAPI. However, most DBAPIs accept more than one paramstyle, and in particular it may be desirable to change a "named" paramstyle into a "positional" one, or vice versa. When this attribute is passed, it should be one of the values "qmark", "numeric", "named", "format" or "pyformat", and should correspond to a parameter style known to be supported by the DBAPI in use. |
pool=None | an already-constructed instance of
~sqlalchemy.pool.Pool , such as a
~sqlalchemy.pool.QueuePool instance. If non-None, this
pool will be used directly as the underlying connection pool
for the engine, bypassing whatever connection parameters are
present in the URL argument. For information on constructing
connection pools manually, see :ref:`pooling_toplevel`. |
poolclass=None | a ~sqlalchemy.pool.Pool
subclass, which will be used to create a connection pool
instance using the connection parameters given in the URL. Note
this differs from pool in that you don't actually
instantiate the pool in this case, you just indicate what type
of pool to be used. |
pool_logging_name | String identifier which will be used within the "name" field of logging records generated within the "sqlalchemy.pool" logger. Defaults to a hexstring of the object's id. See Also :ref:`dbengine_logging` - further detail on how to configure logging. |
pool_pre_ping | boolean, if True will enable the connection pool "pre-ping" feature that tests connections for liveness upon each checkout.
New in version 1.2.
See Also |
pool_size=5 | the number of connections to keep open
inside the connection pool. This used with
~sqlalchemy.pool.QueuePool as
well as ~sqlalchemy.pool.SingletonThreadPool . With
~sqlalchemy.pool.QueuePool , a pool_size setting
of 0 indicates no limit; to disable pooling, set poolclass to
~sqlalchemy.pool.NullPool instead. |
pool_recycle=-1 | this setting causes the pool to recycle connections after the given number of seconds has passed. It defaults to -1, or no timeout. For example, setting to 3600 means connections will be recycled after one hour. Note that MySQL in particular will disconnect automatically if no activity is detected on a connection for eight hours (although this is configurable with the MySQLDB connection itself and the server configuration as well). See Also |
pool_reset_on_return='rollback' | set the
:paramref:`_pool.Pool.reset_on_return` parameter of the underlying
|
pool_timeout=30 | number of seconds to wait before giving
up on getting a connection from the pool. This is only used
with |
pool_use_lifo=False | use LIFO (last-in-first-out) when retrieving
connections from
|
plugins | string list of plugin names to load. See
New in version 1.2.3.
|
query_cache_size | size of the cache used to cache the SQL string form of queries. Set to zero to disable caching. The cache is pruned of its least recently used items when its size reaches N * 1.5. Defaults to 500, meaning the cache will always store at least 500 SQL statements when filled, and will grow up to 750 items at which point it is pruned back down to 500 by removing the 250 least recently used items. Caching is accomplished on a per-statement basis by generating a cache key that represents the statement's structure, then generating string SQL for the current dialect only if that key is not present in the cache. All statements support caching, however some features such as an INSERT with a large set of parameters will intentionally bypass the cache. SQL logging will indicate statistics for each statement whether or not it were pull from the cache. Note some ORM functions related to unit-of-work persistence as well as some attribute loading strategies will make use of individual per-mapper caches outside of the main cache. See Also
New in version 1.4.
|
Create a new Engine instance using a configuration dictionary.
The dictionary is typically produced from a config file.
The keys of interest to engine_from_config() should be prefixed, e.g.
sqlalchemy.url, sqlalchemy.echo, etc. The 'prefix' argument
indicates the prefix to be searched for. Each matching key (after the
prefix is stripped) is treated as though it were the corresponding keyword
argument to a _sa.create_engine
call.
The only required key is (assuming the default prefix) sqlalchemy.url, which provides the :ref:`database URL <database_urls>`.
A select set of keyword arguments will be "coerced" to their expected type based on string values. The set of arguments is extensible per-dialect using the engine_config_types accessor.
Parameters | |
configuration | A dictionary (typically produced from a config file,
but this is not a requirement). Items whose keys start with the value
of 'prefix' will have that prefix stripped, and will then be passed to
_sa.create_engine . |
prefix | Prefix to match and then strip from keys in 'configuration'. |
**kwargs | Each keyword argument to engine_from_config() itself overrides the corresponding item taken from the 'configuration' dictionary. Keyword arguments should not be prefixed. |