|
4 | 4 |
|
5 | 5 | .. _init_app:
|
6 | 6 |
|
7 |
| -Init app |
8 |
| -======== |
| 7 | +Initialize Your Application |
| 8 | +=========================== |
9 | 9 |
|
10 |
| -After you defined all your models, tortoise needs you to init them, in order to create backward relations between models and match your db client with appropriate models. |
| 10 | +After defining all your models, Tortoise ORM requires initialization to create backward relations between models and match your database client with the appropriate models. |
11 | 11 |
|
12 |
| -You can do it like this: |
| 12 | +You can initialize your application like this: |
13 | 13 |
|
14 | 14 | .. code-block:: python3
|
15 | 15 |
|
16 | 16 | from tortoise import Tortoise
|
17 | 17 |
|
18 | 18 | async def init():
|
19 | 19 | # Here we create a SQLite DB using file "db.sqlite3"
|
20 |
| - # also specify the app name of "models" |
21 |
| - # which contain models from "app.models" |
| 20 | + # and specify the app name "models" |
| 21 | + # which contains models from "app.models" |
22 | 22 | await Tortoise.init(
|
23 | 23 | db_url='sqlite://db.sqlite3',
|
24 |
| - modules={'models': ['app.models']} |
| 24 | + modules={'app': ['app.models']} |
25 | 25 | )
|
26 | 26 | # Generate the schema
|
27 | 27 | await Tortoise.generate_schemas()
|
28 |
| - |
29 |
| - |
30 |
| -Here we create connection to SQLite database client and then we discover & initialize models. |
31 | 28 |
|
32 |
| -``generate_schema`` generates schema on empty database, you shouldn't run it on every app init, run it just once, maybe out of your main code. |
33 |
| -There is also the option when generating the schemas to set the ``safe`` parameter to ``True`` which will only insert the tables if they don't already exist. |
34 | 29 |
|
35 |
| -If you define the variable ``__models__`` in the ``app.models`` module (or wherever you specify to load your models from), ``generate_schema`` will use that list, rather than automatically finding models for you. |
| 30 | +This example creates a connection to a SQLite database client and then discovers and initializes your models. The example configures the ``app`` application to load models from the ``app.models`` module. In this example it is a single application, but you can use multiple applications to group models. |
36 | 31 |
|
37 |
| -.. _cleaningup: |
| 32 | +The ``generate_schemas()`` method generates the database schema on an empty database. When generating schemas, you can set the ``safe`` parameter to ``True``, which will only create tables if they don't already exist: |
| 33 | + |
| 34 | +.. code-block:: python3 |
| 35 | +
|
| 36 | + await Tortoise.generate_schemas(safe=True) |
38 | 37 |
|
39 |
| -The Importance of cleaning up |
40 |
| -============================= |
| 38 | +See :ref:`migration` for schema migration tools. |
41 | 39 |
|
42 |
| -Tortoise ORM will keep connections open to external Databases. As an ``asyncio`` Python library, it needs to have the connections closed properly or the Python interpreter may still wait for the completion of said connections. |
| 40 | +If you define a ``__models__`` variable in your ``app.models`` module (or wherever you specify to load your models from), ``generate_schemas()`` will use that list instead of automatically discovering models. |
43 | 41 |
|
44 |
| -To ensure connections are closed please ensure that ``Tortoise.close_connections()`` is called: |
| 42 | +Another way of initializing the application is to use the ``Tortoise.init()`` method with the ``config`` parameter. |
45 | 43 |
|
46 | 44 | .. code-block:: python3
|
47 | 45 |
|
48 |
| - await Tortoise.close_connections() |
| 46 | + CONFIG = { |
| 47 | + "connections": { |
| 48 | + "default": { |
| 49 | + "engine": "tortoise.backends.sqlite", |
| 50 | + "credentials": {"file_path": "default.sqlite3"}, |
| 51 | + }, |
| 52 | + "another_conn": { |
| 53 | + "engine": "tortoise.backends.sqlite", |
| 54 | + "credentials": {"file_path": "another_conn.sqlite3"}, |
| 55 | + }, |
| 56 | + }, |
| 57 | + "apps": { |
| 58 | + "app": {"models": ["app.models"], "default_connection": "default"}, |
| 59 | + "another_app": {"models": ["another_app.models"], "default_connection": "another_conn"}, |
| 60 | + }, |
| 61 | + } |
| 62 | +
|
| 63 | + await Tortoise.init(config=CONFIG) |
| 64 | +
|
49 | 65 |
|
50 |
| -The small helper function ``tortoise.run_async()`` will ensure that connections are closed. |
| 66 | +This way of initializing the application is useful when you want to configure different databases for different applications. This also allows to configure connection routing, see :ref:`router` for more details. Also check out :ref:`contrib_fastapi`, :ref:`contrib_sanic` and documentation on other integrations. |
51 | 67 |
|
52 |
| -Reference |
53 |
| -========= |
| 68 | +.. _cleaningup: |
| 69 | + |
| 70 | +The Importance of Cleaning Up |
| 71 | +================================= |
| 72 | + |
| 73 | +Tortoise ORM maintains open connections to external databases. As an ``asyncio``-based Python library, these connections must be closed properly, or the Python interpreter may continue waiting for their completion. |
| 74 | + |
| 75 | +To ensure connections are properly closed, make sure to call ``Tortoise.close_connections()``: |
54 | 76 |
|
55 |
| -.. automodule:: tortoise |
56 |
| - :members: |
57 |
| - :undoc-members: |
| 77 | +.. code-block:: python3 |
| 78 | +
|
| 79 | + await Tortoise.close_connections() |
58 | 80 |
|
| 81 | +The helper function ``tortoise.run_async()`` automatically ensures that connections are closed when your application terminates. |
0 commit comments