aerich icon indicating copy to clipboard operation
aerich copied to clipboard

tortoise.exceptions.ConfigurationError: No DB associated to model

Open Mr-NB opened this issue 3 years ago • 9 comments

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

image

image

Mr-NB avatar Nov 24 '20 10:11 Mr-NB

+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 avatar Nov 25 '20 15:11 MarkAntipin

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)?

MarkAntipin avatar Nov 27 '20 23:11 MarkAntipin

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

long2ice avatar Nov 28 '20 05:11 long2ice

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.

MarkAntipin avatar Nov 28 '20 11:11 MarkAntipin

That's right

long2ice avatar Nov 28 '20 15:11 long2ice

I second this, because the error is really cryptic.

Nek-12 avatar Jul 23 '21 15:07 Nek-12

I have the same problem!

kostyaten avatar Aug 24 '21 16:08 kostyaten

same

liuyuanjun520 avatar Mar 11 '22 07:03 liuyuanjun520

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`

wiki119 avatar Nov 24 '22 17:11 wiki119