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

Problems with app.models #233

Open ConstantineBer opened 2 years ago

ConstantineBer commented 2 years ago

I have the problem with aerich configs:

I have the app/database.py file with the next content:

TORTOISE_ORM = {
    "connections": {"default": "postgres://..."},
    "apps": {
        "models": {
            "models": ["app.models", "aerich.models"],
            "default_connection": "default",
        },
    },
}

Also I have a python package with tortoise tables located in app/models/.... I'm trying to create first migrations file, using the next commands:

aerich init -t app.database.TORTOISE_ORM and I'm getting data inside of my pyproject.toml:

[tool.aerich]
tortoise_orm = "app.database.TORTOISE_ORM"
location = "./migrations"
src_folder = "./."

After it I'm trying to run aerich init-db, but I'm getting an error: tortoise.exceptions.ConfigurationError: Module "app.models" not found I tried a lot of different variants to change this configs but getting the same error. Version of aerich==^0.6.2 Does everybody know what's wrong?

grondman commented 1 year ago

Just ran into this tortoise.exceptions.ConfigurationError: Module <x> not found error myself. Using a debugger, I found out that the problem - at least in my case - was actually not that the module could not be found. The ImportError raised (which leads to the ConfigurationError) was in fact hinting towards a problem with circular imports. Weirdly enough I don't get this circular import problem when using the app itself (which runs the same imports), but I do get it when using aerich...

Long story short:

  1. Solving circular imports might do the trick here.
  2. Tortoise should choose a different error message (e.g. just use the msg from the ImportError) as "Module not found" might simply not be the actual problem. I'll raise a pull request later today for this.