tortoise / aerich

A database migrations tool for TortoiseORM, ready to production.
https://github.com/tortoise/aerich
Apache License 2.0
808 stars 91 forks source link

aerich doesn't see model changes in different apps (Tortoise + FastAPI) #210

Open greedWizard opened 2 years ago

greedWizard commented 2 years ago

In app folder I have somethings organized like this: main_app --app1 ----models.py ----views.py ----...... --app2 ----models.py ----views.py ----..... --core ----config.py ----tortoise.py --main.py

In my tortoise.py file I have the tortoise dict:

TORTOISE_ORM = {
    "connections": {"default": config.DB_CONNECTION_STRING},
    "apps": {
        "models": {
            "models": [
                "aerich.models",
            ],
            "default_connection": "default",
        },
        "app1": {
            "models": [
                'app1.models',
            ],
            "default_connection": "default",
        },
        "app2": {
            "models": [
                'app2.models',
            ],
            "default_connection": "default",
        },
    },
}

When I run aerich init-db it's ok. Aerich sees all my models and try to create tables. Then I make some changes either in app1.models or app2.models and run aerich migrate I get no changes detected However if I put the app that I make change in on first place in "apps" field in the tortoise.py config it works fine. For example: tortoise.py:

TORTOISE_ORM = {
    "connections": {"default": config.DB_CONNECTION_STRING},
    "apps": {
        "models": {
            "models": [
                "aerich.models",
            ],
            "default_connection": "default",
        },
        "app1": {
            "models": [
                'app1.models',
            ],
            "default_connection": "default",
        },
        "app2": {
            "models": [
                'app2.models',
            ],
            "default_connection": "default",
        },
    },
}
aerich init-db # works fine
*make some changes in either app2 or app1 models*
aerich migrate # no changes detected

2nd example: tortoise.py

TORTOISE_ORM = {
    "connections": {"default": config.DB_CONNECTION_STRING},
    "apps": {
        "app1": {
            "models": [
                'app1.models',
            ],
            "default_connection": "default",
        },
       "models": {
            "models": [
                "aerich.models",
            ],
            "default_connection": "default",
        },
        "app2": {
            "models": [
                'app2.models',
            ],
            "default_connection": "default",
        },
    },
}
aerich init-db # works fine all my tables in place
*make some changes in app1 and app2*
aerich migrate # only changes in app1 detected

3rd example: tortoise.py

TORTOISE_ORM = {
    "connections": {"default": config.DB_CONNECTION_STRING},
    "apps": {
        "app2": {
            "models": [
                'app2.models',
            ],
            "default_connection": "default",
        },
       "models": {
            "models": [
                "aerich.models",
            ],
            "default_connection": "default",
        },
        "app1": {
            "models": [
                'app1.models',
            ],
            "default_connection": "default",
        },
    },
}
aerich init-db # works fine all my tables in place
*make some changes in app1 and app2*
aerich migrate # only changes in app2 detected

Seems like aerich only sees changes in the app at the first place in "app" field. I have a workaround, I just put all apps in one dict like:

TORTOISE_ORM = {
    "connections": {"default": config.DB_CONNECTION_STRING},
    "apps": {
        "models": {
            "models": [
                "aerich.models",
                'app1.models',
                'app2.models',
            ],
            "default_connection": "default",
        },
    },
}

and change all my app1.MyModel and app2.MyModel references in relational fields in models to models.MyModel run migrations and it works fine. However it's too annoying to change that all the time when I want to make migrations. I think I described my problem clear. Any idea what do I do wrong or is it really an issue? If that's so I'd like to hope that this one will be fixed soon.

Jamba777 commented 2 years ago

I have the same trouble with Sanic integration