| 1 |
Multiple Databases with Binds |
| 2 |
============================= |
| 3 |
|
| 4 |
SQLAlchemy can connect to more than one database at a time. It refers to different |
| 5 |
engines as "binds". Flask-SQLAlchemy simplifies how binds work by associating each |
| 6 |
engine with a short string, a "bind key", and then associating each model and table with |
| 7 |
a bind key. The session will choose what engine to use for a query based on the bind key |
| 8 |
of the thing being queried. If no bind key is given, the default engine is used. |
| 9 |
|
| 10 |
|
| 11 |
Configuring Binds |
| 12 |
----------------- |
| 13 |
|
| 14 |
The default bind is still configured by setting :data:`.SQLALCHEMY_DATABASE_URI`, and |
| 15 |
:data:`.SQLALCHEMY_ENGINE_OPTIONS` for any engine options. Additional binds are given in |
| 16 |
:data:`.SQLALCHEMY_BINDS`, a dict mapping bind keys to engine URLs. To specify engine |
| 17 |
options for a bind, the value can be a dict of engine options with the ``"url"`` key, |
| 18 |
instead of only a URL string. |
| 19 |
|
| 20 |
.. code-block:: python |
| 21 |
|
| 22 |
SQLALCHEMY_DATABASE_URI = "postgresql:///main" |
| 23 |
SQLALCHEMY_BINDS = { |
| 24 |
"meta": "sqlite:////path/to/meta.db", |
| 25 |
"auth": { |
| 26 |
"url": "mysql://localhost/users", |
| 27 |
"pool_recycle": 3600, |
| 28 |
}, |
| 29 |
} |
| 30 |
|
| 31 |
|
| 32 |
Defining Models and Tables with Binds |
| 33 |
------------------------------------- |
| 34 |
|
| 35 |
Flask-SQLAlchemy will create a metadata and engine for each configured bind. Models and |
| 36 |
tables with a bind key will be registered with the corresponding metadata, and the |
| 37 |
session will query them using the corresponding engine. |
| 38 |
|
| 39 |
To set the bind for a model, set the ``__bind_key__`` class attribute. Not setting a |
| 40 |
bind key is equivalent to setting it to ``None``, the default key. |
| 41 |
|
| 42 |
.. code-block:: python |
| 43 |
|
| 44 |
class User(db.Model): |
| 45 |
__bind_key__ = "auth" |
| 46 |
id = db.Column(db.Integer, primary_key=True) |
| 47 |
|
| 48 |
Models that inherit from this model will share the same bind key, or can override it. |
| 49 |
|
| 50 |
To set the bind for a table, pass the ``bind_key`` keyword argument. |
| 51 |
|
| 52 |
.. code-block:: python |
| 53 |
|
| 54 |
user_table = db.Table( |
| 55 |
"user", |
| 56 |
db.Column("id", db.Integer, primary_key=True), |
| 57 |
bind_key="auth", |
| 58 |
) |
| 59 |
|
| 60 |
Ultimately, the session looks up the bind key on the metadata associated with the model |
| 61 |
or table. That association happens during creation. Therefore, changing the bind key |
| 62 |
after creating a model or table will have no effect. |
| 63 |
|
| 64 |
|
| 65 |
Accessing Metadata and Engines |
| 66 |
------------------------------ |
| 67 |
|
| 68 |
You may need to inspect the metadata or engine for a bind. Note that you should execute |
| 69 |
queries through the session, not directly on the engine. |
| 70 |
|
| 71 |
The default engine is :attr:`.SQLAlchemy.engine`, and the default metadata is |
| 72 |
:attr:`.SQLAlchemy.metadata`. :attr:`.SQLAlchemy.engines` and |
| 73 |
:attr:`.SQLAlchemy.metadatas` are dicts mapping all bind keys. |
| 74 |
|
| 75 |
|
| 76 |
Creating and Dropping Tables |
| 77 |
---------------------------- |
| 78 |
|
| 79 |
The :meth:`~.SQLAlchemy.create_all` and :meth:`~.SQLAlchemy.drop_all` methods operate on |
| 80 |
all binds by default. The ``bind_key`` argument to these methods can be a string or |
| 81 |
``None`` to operate on a single bind, or a list of strings or ``None`` to operate on a |
| 82 |
subset of binds. Because these methods access the engines, they must be called inside an |
| 83 |
application context. |
| 84 |
|
| 85 |
.. code-block:: python |
| 86 |
|
| 87 |
# create tables for all binds |
| 88 |
db.create_all() |
| 89 |
|
| 90 |
# create tables for the default and "auth" binds |
| 91 |
db.create_all(bind_key=[None, "auth"]) |
| 92 |
|
| 93 |
# create tables for the "meta" bind |
| 94 |
db.create_all(bind_key="meta") |
| 95 |
|
| 96 |
# drop tables for the default bind |
| 97 |
db.drop_all(bind_key=None) |