tortoise / tortoise/tortoise-orm

The "apps" configuration directive is extremely confusing

Open
#851 3 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
5.6k
Forks
516
Avg merge
2d 21h
Merged PRs (30d)
9

Description

Hi,
I am having a lot of trouble figuring out how the "apps" section of tortoise config is supposed to be set up. I have a project layout that looks like this:

├── app
│   ├── config.py
│   ├── db.py
│   ├── main.py
│   └── targets
│       ├── __init__.py
│       ├── models.py
├── README.md
├── requirements.txt
├── setup.py
└── tests

There are Tortoise models defined in app.targets.models

My Tortoise config object looks like this:

{
    'connections': {
        'default': settings.DATABASE_URL
    },
    'apps': {
        'targets:': {
            'models': ['app.targets.models'],
        }
    }

And then I register tortoise like so:

fastapi.register_tortoise(
    app=app,
    config=settings.TORTOISE,
    generate_schemas=True,
    add_exception_handlers=True
)

But tortoise does not find the models:

INFO:     Waiting for application startup.
/home/austin/.virtualenvs/sdexchange-server/lib/python3.9/site-packages/tortoise/__init__.py:601: RuntimeWarning: Module "app.targets.models" has no models
  cls._init_apps(apps_config)
INFO:     Tortoise-ORM started, {'default': <tortoise.backends.asyncpg.client.AsyncpgDBClient object at 0x7fa7a5f275b0>}, {'targets:': {}}

I've tried the following things:

Adding app = 'Targes' to the Meta class of each model.
Adding __models__=[Model1, Model2] to both models.py and app.targets.__init__.py
Changing the path in the models config array to app.targets.

None of these attempts work.

However, if I use different keyword arguments for register_tortoise, Tortoise finds the models:

fastapi.register_tortoise(
    app=app,
    db_url=settings.DATABASE_URL,
    modules={'targets': ['app.targets.models']},
    generate_schemas=True,
    add_exception_handlers=True
)

Are we only supposed to use the modules keyword argument for register_tortoise? In what circumstance should we be using the tortoise config object? For example in the documenation for Aerich it says to add aerich.models to the tortoise config, which seems to be at odds with how you set up tortoise for FastAPI.

Any thoughts?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the FastAPI register_tortoise configuration shown in the issue and compare it with the Aerich usage documentation linked there. Trace how the apps and modules settings are interpreted, then clarify the documented configuration examples and explain when each form should be used.

Written by the indexing model from the issue text.

Assessment

Tech stack
fastapi, python
Domain
documentation
Issue type
Documentation
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.