tortoise / aerich

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

tortoise.exceptions.ConfigurationError: No DB associated to model #88

Open Mr-NB opened 3 years ago

Mr-NB commented 3 years ago

When I execute aerich init-db, there is an error tortoise.exceptions.ConfigurationError: No DB associated to model

image

image

MarkAntipin commented 3 years ago

+1 I have almost the same issue.

When I run aerich init-db on clean database (PostgresSQL), i get an error tortoise.exceptions.ConfigurationError: No DB associated to model , but the database is initializing.

When I run aerich migrate --name {some_name} on already initialized database, then error tortoise.exceptions.ConfigurationError: No DB associated to model happened again, and no changes applied to database

My tortoise config:

DB_CONN = {
    'connections': {
        'default': {
            'engine': 'tortoise.backends.asyncpg',
            'credentials': {
                'host': 'localhost',
                'port': 5432,
                'user': 'postgres',
                'password': 'postgres',
                'database': 'some-db',
        },
        }
    },
    'apps': {
        'models': {
            'models': ['models'],
            'default_connection': 'default'
        }
    }
}

And models file:

from tortoise.models import Model
from tortoise.fields import IntField, CharField

class SomeModel(Model):
    id = IntField(pk=True)
    some_field = CharField(max_length=128)
    some_field_new = CharField(max_length=128, null=True)

Using latest packages: aerich = "^0.3.3" tortoise-orm = "^0.16.18"

MarkAntipin commented 3 years ago

Sorry, I didn't notice that adding aerich.models to the configuration is a required condition. But you don't think it's better to add it in the aerich itself?

Most people add migrations to an existing project and do not rely on the fact that they will have to change something in the database conf file.

What about adding something like:

list(tortoise_config['apps'].values())[0]['models'].append('aerich.models')

in your cli() function (63 line in aerich.cli.py)?

long2ice commented 3 years ago

So what about multiple databases? The first database may don't wan't be choice to store aerich

MarkAntipin commented 3 years ago

Yes, that's true, but you can add some kind of error, like:

if 'aerich.models' not in [m for model in tortoise_config['apps'].values() for m in model['models']]:
    raise ConfigurationError("You have to add 'aerich.models' in your models")

This will make it much clearer what needs to be done for aerich to work.

long2ice commented 3 years ago

That's right

Nek-12 commented 3 years ago

I second this, because the error is really cryptic.

kostyaten commented 3 years ago

I have the same problem!

liuyuanjun520 commented 2 years ago

same

wiki119 commented 1 year ago

Hello, I found this post from google results. I'm having the same exception in a clustermaps scraper script. The answers on stackoverflow are specific to their scripts. Could you take a look at this if it's library issue or something i need to do?

`C:\Users\Administrator\Desktop\scraper clustrmaps>python start.py Task exception was never retrieved future: <Task finished name='Task-2' coro=<stats_loop() done, defined at C:\Users\Administrator\Desktop\scraper clustrmaps\controller.py:55> exception=ConfigurationError('No DB associated to model')> Traceback (most recent call last): File "C:\Users\Administrator\AppData\Local\Programs\Python\Python38\lib\site-packages\tortoise\models.py", line 262, in db return current_transaction_map[self.default_connection].get() KeyError: None

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "C:\Users\Administrator\Desktop\scraper clustrmaps\controller.py", line 59, in stats_loop stats = await db_stats() File "C:\Users\Administrator\Desktop\scraper clustrmaps\controller.py", line 47, in db_stats 'total': await Address.all().count(), File "C:\Users\Administrator\AppData\Local\Programs\Python\Python38\lib\site-packages\tortoise\models.py", line 1056, in all return QuerySet(cls) File "C:\Users\Administrator\AppData\Local\Programs\Python\Python38\lib\site-packages\tortoise\queryset.py", line 255, in init super().init(model) File "C:\Users\Administrator\AppData\Local\Programs\Python\Python38\lib\site-packages\tortoise\queryset.py", line 88, in init self.capabilities: Capabilities = model._meta.db.capabilities File "C:\Users\Administrator\AppData\Local\Programs\Python\Python38\lib\site-packages\tortoise\models.py", line 264, in db raise ConfigurationError("No DB associated to model") tortoise.exceptions.ConfigurationError: No DB associated to model Traceback (most recent call last): File "C:\Users\Administrator\AppData\Local\Programs\Python\Python38\lib\site-packages\tortoise\models.py", line 262, in db return current_transaction_map[self.default_connection].get() KeyError: None

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "start.py", line 38, in loop.run_until_complete(start()) File "C:\Users\Administrator\AppData\Local\Programs\Python\Python38\lib\asyncio\base_events.py", line 616, in run_until_complete return future.result() File "start.py", line 34, in start await controller.start(args.threads) File "C:\Users\Administrator\Desktop\scraper clustrmaps\controller.py", line 104, in start await multithread(threads) File "C:\Users\Administrator\Desktop\scraper clustrmaps\controller.py", line 82, in multithread done = await check_done() File "C:\Users\Administrator\Desktop\scraper clustrmaps\controller.py", line 75, in check_done stats = await db_stats() File "C:\Users\Administrator\Desktop\scraper clustrmaps\controller.py", line 47, in db_stats 'total': await Address.all().count(), File "C:\Users\Administrator\AppData\Local\Programs\Python\Python38\lib\site-packages\tortoise\models.py", line 1056, in all return QuerySet(cls) File "C:\Users\Administrator\AppData\Local\Programs\Python\Python38\lib\site-packages\tortoise\queryset.py", line 255, in init super().init(model) File "C:\Users\Administrator\AppData\Local\Programs\Python\Python38\lib\site-packages\tortoise\queryset.py", line 88, in init self.capabilities: Capabilities = model._meta.db.capabilities File "C:\Users\Administrator\AppData\Local\Programs\Python\Python38\lib\site-packages\tortoise\models.py", line 264, in db raise ConfigurationError("No DB associated to model") tortoise.exceptions.ConfigurationError: No DB associated to model`