简体   繁体   English

运行测试时不存在 Django 关系

[英]Django relation does not exist when running test

I'm using Django 2.2.3 and Postgres.我正在使用 Django 2.2.3和 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 .我正在尝试将测试添加到项目中,并且在执行django.db.utils.ProgrammingError: relation "auth_group" does not exist python./manage.py testpython./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.首先,我的建议是我稍微弄乱了我的数据库架构——前段时间开发团队被要求从另一个数据库导入几个表,我已经通过使用 pgsql 直接导入来完成它。 Than I created models using python manage.py inspectdb and placed them into a separate app.然后我使用python manage.py inspectdb创建模型并将它们放入单独的应用程序中。 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.经过简短调查后,我发现, inspectdb还为一些 auth 表创建了模型,并且它们存在于具有旧表的应用程序的 models.py 中。 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:在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.我认为,这可能与一个问题有关,但我试图从INSTALLED_APPS中删除该应用程序,完全删除该应用程序,但没有任何效果。 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. PS这个问题与South package的问题无关,因为它没有在这个项目中使用。

Thanks.谢谢。

PPS INSTALLED_APPS : 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:我发现这个问题在某种程度上与自定义用户 model 有关,它是通过以下方式声明的:

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.事实证明,将其替换为默认的 Django 用户修复了该问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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