Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RuntimeWarning: Module "x.x.x" has no models #352

Open
1451872028 opened this issue Jul 17, 2024 · 1 comment
Open

RuntimeWarning: Module "x.x.x" has no models #352

1451872028 opened this issue Jul 17, 2024 · 1 comment

Comments

@1451872028
Copy link

HI.

I am building a service on FastAPI using Tortoise-ORM.
The problem is that in this case it is not possible to find the models under the module.

I always get this warning from Tortoise
./flexipay/lib/python3.8/site-packages/tortoise/__init__.py:514: RuntimeWarning: Module "apps.res_users.models" has no models

I am unable to create the correct migration and have no previous migration history.

config:

DATABASE_CONFIG: Dict = {
        'connections': {
            'default': DATABASE_URL
        },
        'apps': {
            'models': {
                'default_connection': 'default',
                'models': ['apps.res_users.models', 'aerich.models']
            },
        },
    }

init_db.py

import asyncio
from tortoise import Tortoise
from core.settings import settings


async def init():
    await Tortoise.init(config=settings.DATABASE_CONFIG)
    await Tortoise.generate_schemas()
    await Tortoise.close_connections()

if __name__ == "__main__":
    asyncio.run(init())

It only creates the "aerich" table.

@Abeautifulsnow
Copy link

Abeautifulsnow commented Aug 6, 2024

What is your demand? If you want to apply your changes to the database, why not use aerich cli? I have a project structure imilar to yours. My aerich version is 0.7.2.

  • settings.py
TORTOISE_ORM = {
    "connections": {"default": DATABASE_URL},
    "apps": {
        "models": {
            "models": ["manager.govern.models", "aerich.models"],
            "default_connection": "default",
        },
    },
}

And this is my models.py contents:

  • models.py
from tortoise import fields, models


class BaseModel(models.Model):
    """
    The `base_model` model
    """

    update_time = fields.DatetimeField(auto_now=True)
    create_time = fields.DatetimeField(auto_now_add=True)

    class Meta:
        abstract = True

class OtherModels(models.Model):
    ....

Then I execute aerich command in the settings.py sibling directory to generate migrations folder and migrate & upgrade changed files to specify version, every thing is ok. Official Usage

After that and I have some new files in migrations/models folder now:

migrations
└── models
    ├── 1_20240805215208_add_global_id.py
    └── 0_20240805203911_init.py

Summary: Total folders: 1 Total files: 2 Total size: 4.8KiB

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants