简体   繁体   中英

Django relation does not exist when running test

I'm using Django 2.2.3 and Postgres.

I'm trying to add tests into the project and constantly running into an error django.db.utils.ProgrammingError: relation "auth_group" does not exist at test db creation step, when doing either python./manage.py test or python./manage.py test app_name .

First of all, my suggestion is that I've messed my db schema a little - some time ago development team was asked to import several tables from another db and I've done it via direct importing using pgsql. Than I created models using python manage.py inspectdb and placed them into a separate app. That command created an initial migration, which looks like that:

class Migration(migrations.Migration):

initial = True

dependencies = []

operations = [
    migrations.CreateModel(
        name='SomeModelName',
        fields=[
            (
                'id',
                models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID'),
            ),
            ('some_other_field)', models.CharField(max_length=254, unique=True)),
        ],
        options={'db_table': 'some_existing_table_name', 'managed': False,},
    ),

Everything works fine, since than I've created and applied several migrations without any issues. But now I've got stuck with that tests issue.

After a brief investigation I found, that inspectdb also created models for some auth tables, and they are present in models.py of that apps with old tables. So, the following lines are present into initial migration of the discussed app:

        migrations.CreateModel(
        name='AuthGroup',
        fields=[
            (
                'id',
                models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID'),
            ),
            ('name', models.CharField(max_length=80, unique=True)),
        ],
        options={'db_table': 'auth_group', 'managed': False,},
    ),

and in models.py:

class AuthGroup(models.Model):
name = models.CharField(unique=True, max_length=80)

class Meta:
    managed = False
    db_table = 'auth_group'

I think, that this may be related to an issue, but I've tried to removing that app from INSTALLED_APPS , deleting that app at all, but nothing worked. I still get those errors while running tests

Any ideas, what can I do to fix this issue? Maybe, this behavior is caused by something else?

PS this issue is unrelated to problems with South package, because it's not used in this project.

Thanks.

PPS INSTALLED_APPS :

INSTALLED_APPS = (
[
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]
+ PROJECT_APPS
+ THIRD_PARTY_APPS
)

I found out that the problem was somehow related to custom user model, which was declared the following way:

from django.contrib.auth.models import AbstractUser

class User(AbstractUser):
    class Meta:
        db_table = 'auth_user'

It turned out that replacing it with default Django User fixed the issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM