| 1 |
Configuration |
| 2 |
============= |
| 3 |
|
| 4 |
|
| 5 |
Configuration Keys |
| 6 |
------------------ |
| 7 |
|
| 8 |
Configuration is loaded from the Flask ``app.config`` when :meth:`.SQLAlchemy.init_app` |
| 9 |
is called. The configuration is not read again after that. Therefore, all configuration |
| 10 |
must happen before initializing the application. |
| 11 |
|
| 12 |
.. module:: flask_sqlalchemy.config |
| 13 |
|
| 14 |
.. data:: SQLALCHEMY_DATABASE_URI |
| 15 |
|
| 16 |
The database connection URI used for the default engine. It can be either a string |
| 17 |
or a SQLAlchemy ``URL`` instance. See below and :external:doc:`core/engines` for |
| 18 |
examples. |
| 19 |
|
| 20 |
At least one of this and :data:`SQLALCHEMY_BINDS` must be set. |
| 21 |
|
| 22 |
.. versionchanged:: 3.0 |
| 23 |
No longer defaults to an in-memory SQLite database if not set. |
| 24 |
|
| 25 |
.. data:: SQLALCHEMY_ENGINE_OPTIONS |
| 26 |
|
| 27 |
A dict of arguments to pass to :func:`sqlalchemy.create_engine` for the default |
| 28 |
engine. |
| 29 |
|
| 30 |
This takes precedence over the ``engine_options`` argument to :class:`.SQLAlchemy`, |
| 31 |
which can be used to set default options for all engines. |
| 32 |
|
| 33 |
.. versionchanged:: 3.0 |
| 34 |
Only applies to the default bind. |
| 35 |
|
| 36 |
.. versionadded:: 2.4 |
| 37 |
|
| 38 |
.. data:: SQLALCHEMY_BINDS |
| 39 |
|
| 40 |
A dict mapping bind keys to engine options. The value can be a string or a |
| 41 |
SQLAlchemy ``URL`` instance. Or it can be a dict of arguments, including the ``url`` |
| 42 |
key, that will be passed to :func:`sqlalchemy.create_engine`. The ``None`` key can |
| 43 |
be used to configure the default bind, but :data:`SQLALCHEMY_ENGINE_OPTIONS` and |
| 44 |
:data:`SQLALCHEMY_DATABASE_URI` take precedence. |
| 45 |
|
| 46 |
At least one of this and :data:`SQLALCHEMY_DATABASE_URI` must be set. |
| 47 |
|
| 48 |
.. versionadded:: 0.12 |
| 49 |
|
| 50 |
.. data:: SQLALCHEMY_ECHO |
| 51 |
|
| 52 |
The default value for ``echo`` and ``echo_pool`` for every engine. This is useful to |
| 53 |
quickly debug the connections and queries issued from SQLAlchemy. |
| 54 |
|
| 55 |
.. versionchanged:: 3.0 |
| 56 |
Sets ``echo_pool`` in addition to ``echo``. |
| 57 |
|
| 58 |
.. data:: SQLALCHEMY_RECORD_QUERIES |
| 59 |
|
| 60 |
If enabled, information about each query during a request will be recorded. Use |
| 61 |
:func:`.get_recorded_queries` to get a list of queries that were issued during the |
| 62 |
request. |
| 63 |
|
| 64 |
.. versionchanged:: 3.0 |
| 65 |
Not enabled automatically in debug or testing mode. |
| 66 |
|
| 67 |
.. data:: SQLALCHEMY_TRACK_MODIFICATIONS |
| 68 |
|
| 69 |
If enabled, all ``insert``, ``update``, and ``delete`` operations on models are |
| 70 |
recorded, then sent in :data:`.models_committed` and |
| 71 |
:data:`.before_models_committed` signals when ``session.commit()`` is called. |
| 72 |
|
| 73 |
This adds a significant amount of overhead to every session. Prefer using |
| 74 |
SQLAlchemy's :external:doc:`orm/events` directly for the exact information you need. |
| 75 |
|
| 76 |
.. versionchanged:: 3.0 |
| 77 |
Disabled by default. |
| 78 |
|
| 79 |
.. versionadded:: 2.0 |
| 80 |
|
| 81 |
.. versionchanged:: 3.1 |
| 82 |
Removed ``SQLALCHEMY_COMMIT_ON_TEARDOWN``. |
| 83 |
|
| 84 |
.. versionchanged:: 3.0 |
| 85 |
Removed ``SQLALCHEMY_NATIVE_UNICODE``, ``SQLALCHEMY_POOL_SIZE``, |
| 86 |
``SQLALCHEMY_POOL_TIMEOUT``, ``SQLALCHEMY_POOL_RECYCLE``, and |
| 87 |
``SQLALCHEMY_MAX_OVERFLOW``. |
| 88 |
|
| 89 |
|
| 90 |
Connection URL Format |
| 91 |
--------------------- |
| 92 |
|
| 93 |
See SQLAlchemy's documentation on :external:doc:`core/engines` for a complete |
| 94 |
description of syntax, dialects, and options. |
| 95 |
|
| 96 |
A basic database connection URL uses the following format. Username, password, host, and |
| 97 |
port are optional depending on the database type and configuration. |
| 98 |
|
| 99 |
.. code-block:: text |
| 100 |
|
| 101 |
dialect://username:password@host:port/database |
| 102 |
|
| 103 |
Here are some example connection strings: |
| 104 |
|
| 105 |
.. code-block:: text |
| 106 |
|
| 107 |
# SQLite, relative to Flask instance path |
| 108 |
sqlite:///project.db |
| 109 |
|
| 110 |
# PostgreSQL |
| 111 |
postgresql://scott:tiger@localhost/project |
| 112 |
|
| 113 |
# MySQL / MariaDB |
| 114 |
mysql://scott:tiger@localhost/project |
| 115 |
|
| 116 |
SQLite does not use a user or host, so its URLs always start with _three_ slashes |
| 117 |
instead of two. The ``dbname`` value is a file path. Absolute paths start with a |
| 118 |
_fourth_ slash (on Linux or Mac). Relative paths are relative to the Flask application's |
| 119 |
:attr:`~flask.Flask.instance_path`. |
| 120 |
|
| 121 |
|
| 122 |
Default Driver Options |
| 123 |
---------------------- |
| 124 |
|
| 125 |
Some default options are set for SQLite and MySQL engines to make them more usable by |
| 126 |
default in web applications. |
| 127 |
|
| 128 |
SQLite relative file paths are relative to the Flask instance path instead of the |
| 129 |
current working directory. In-memory databases use a static pool and |
| 130 |
``check_same_thread`` to work across requests. |
| 131 |
|
| 132 |
MySQL (and MariaDB) servers are configured to drop connections that have been idle for |
| 133 |
8 hours, which can result in an error like ``2013: Lost connection to MySQL server |
| 134 |
during query``. A default ``pool_recycle`` value of 2 hours (7200 seconds) is used to |
| 135 |
recreate connections before that timeout. |
| 136 |
|
| 137 |
|
| 138 |
Engine Configuration Precedence |
| 139 |
------------------------------- |
| 140 |
|
| 141 |
Because Flask-SQLAlchemy has support for multiple engines, there are rules for which |
| 142 |
config overrides other config. Most applications will only have a single database and |
| 143 |
only need to use :data:`SQLALCHEMY_DATABASE_URI` and :data:`SQLALCHEMY_ENGINE_OPTIONS`. |
| 144 |
|
| 145 |
- If the ``engine_options`` argument is given to :class:`.SQLAlchemy`, it sets default |
| 146 |
options for *all* engines. :data:`SQLALCHEMY_ECHO` sets the default value for both |
| 147 |
``echo`` and ``echo_pool`` for all engines. |
| 148 |
- The options for each engine in :data:`.SQLALCHEMY_BINDS` override those defaults. |
| 149 |
- :data:`.SQLALCHEMY_ENGINE_OPTIONS` overrides the ``None`` key in |
| 150 |
``SQLALCHEMY_BINDS``, and :data:`.SQLALCHEMY_DATABASE_URI` overrides the ``url`` key |
| 151 |
in that engine's options. |
| 152 |
|
| 153 |
|
| 154 |
Timeouts |
| 155 |
-------- |
| 156 |
|
| 157 |
Certain databases may be configured to close inactive connections after a period of |
| 158 |
time. MySQL and MariaDB are configured for this by default, but database services may |
| 159 |
also configure this type of limit. This can result in an error like |
| 160 |
``2013: Lost connection to MySQL server during query``. |
| 161 |
|
| 162 |
If you encounter this error, try setting ``pool_recycle`` in the engine options to |
| 163 |
a value less than the database's timeout. |
| 164 |
|
| 165 |
Alternatively, you can try setting ``pool_pre_ping`` if you expect the database to close |
| 166 |
connections often, such as if it's running in a container that may restart. |
| 167 |
|
| 168 |
See SQAlchemy's docs on `dealing with disconnects`_ for more information. |
| 169 |
|
| 170 |
.. _dealing with disconnects: https://docs.sqlalchemy.org/core/pooling.html#dealing-with-disconnects |