The following configuration values exist for Flask-SQLAlchemy.Flask-SQLAlchemy loads these values from your main Flask config which canbe populated in various ways. Note that some of those cannot be modifiedafter the engine was created so make sure to configure as early aspossible and to not modify them at runtime.
Configuration Keys¶
On a Mac or on Linux, the command to do this is export FLASKAPP=application.py. On Windows, the command is instead set FLASKAPP=application.py. You may optionally want to set the environment variable FLASKDEBUG to 1, which will activate Flask’s debugger and will automatically reload your web application whenever you save a change to a file. From flask import Flask app = Flask(name) @app.route('/') def helloworld: return 'Hello World’ if name 'main': app.run Importing flask module in the project is mandatory. An object of Flask class is our WSGI application. Flask constructor takes the name of current module (name) as argument. The route function of the.
A list of configuration keys currently understood by the extension:
| The database URI that should be used forthe connection. Examples:
|
| A dictionary that maps bind keys toSQLAlchemy connection URIs. For moreinformation about binds see Multiple Databases with Binds. |
| If set to True SQLAlchemy will log allthe statements issued to stderr which canbe useful for debugging. |
| Can be used to explicitly disable orenable query recording. Query recordingautomatically happens in debug or testingmode. See |
| Can be used to explicitly disable nativeunicode support. This is required forsome database adapters (like PostgreSQLon some Ubuntu versions) when used withimproper database defaults that specifyencoding-less databases. Deprecated as of v2.4 and will be removed in v3.0. |
| The size of the database pool. Defaultsto the engine’s default (usually 5). Deprecated as of v2.4 and will be removed in v3.0. |
| Specifies the connection timeout in secondsfor the pool. Deprecated as of v2.4 and will be removed in v3.0. |
| Number of seconds after which aconnection is automatically recycled.This is required for MySQL, which removesconnections after 8 hours idle bydefault. Note that Flask-SQLAlchemyautomatically sets this to 2 hours ifMySQL is used. Some backends may use adifferent default timeout value. For moreinformation about timeouts seeTimeouts. Deprecated as of v2.4 and will be removed in v3.0. |
| Controls the number of connections thatcan be created after the pool reachedits maximum size. When those additionalconnections are returned to the pool,they are disconnected and discarded. Deprecated as of v2.4 and will be removed in v3.0. |
| If set to |
| A dictionary of keyword args to send to |
New in version 0.8: The SQLALCHEMY_NATIVE_UNICODE
, SQLALCHEMY_POOL_SIZE
,SQLALCHEMY_POOL_TIMEOUT
and SQLALCHEMY_POOL_RECYCLE
configuration keys were added.
Set Flask App Mac Os
New in version 0.12: The SQLALCHEMY_BINDS
configuration key was added.
New in version 0.17: The SQLALCHEMY_MAX_OVERFLOW
configuration key was added.
New in version 2.0: The SQLALCHEMY_TRACK_MODIFICATIONS
configuration key was added.
Changed in version 2.1: SQLALCHEMY_TRACK_MODIFICATIONS
will warn if unset.
Changed in version 2.4: * SQLALCHEMY_ENGINE_OPTIONS
configuration key was added.* Deprecated keys
SQLALCHEMY_NATIVE_UNICODE
SQLALCHEMY_POOL_SIZE
SQLALCHEMY_POOL_TIMEOUT
SQLALCHEMY_POOL_RECYCLE
SQLALCHEMY_MAX_OVERFLOW
Changed in version 2.4.3: Deprecated SQLALCHEMY_COMMIT_ON_TEARDOWN
.
Connection URI Format¶
For a complete list of connection URIs head over to the SQLAlchemydocumentation under (Supported Databases). This here showssome common connection strings.
SQLAlchemy indicates the source of an Engine as a URI combined withoptional keyword arguments to specify options for the Engine. The form ofthe URI is:
Many of the parts in the string are optional. If no driver is specifiedthe default one is selected (make sure to not include the +
in thatcase).
Postgres:
MySQL:
Oracle:
SQLite (note that platform path conventions apply):
Using custom MetaData and naming conventions¶
You can optionally construct the SQLAlchemy
object with a customMetaData
object.This allows you to, among other things,specify a custom constraint naming conventionin conjunction with SQLAlchemy 0.9.2 or higher.Doing so is important for dealing with database migrations (for instance usingalembic as statedhere. Here’s anexample, as suggested by the SQLAlchemy docs:
For more info about MetaData
,check out the official docs on it.
Set Flask App Mac Pro
Timeouts¶
Certain database backends may impose different inactive connection timeouts,which interferes with Flask-SQLAlchemy’s connection pooling.
By default, MariaDB is configured to have a 600 second timeout. This oftensurfaces hard to debug, production environment only exceptions like 2013:LostconnectiontoMySQLserverduringquery
.
Set Flask App Mac Download
If you are using a backend (or a pre-configured database-as-a-service) with alower connection timeout, it is recommended that you setSQLALCHEMY_POOL_RECYCLE to a value less than your backend’s timeout.