Django 测试错误:关系不存在

问题描述 投票:0回答:3

我在 Ubuntu 13.10 和 Postgres 上使用 Django 1.6 和 Python 3.3。

我有一个模型

User
定义如下:

from django.db import models
from django.contrib.auth.models import AbstractUser

from common.mixins import TimestampMixin


class User(TimestampMixin, AbstractUser):
    auth_key = models.CharField(max_length=16)

    def __str__(self):
        if self.first_name:
            if self.last_name:
                return "{0} {1}'s Profile".format(
                    self.first_name, self.last_name)
            else:
                return "{0}'s Profile".format(self.first_name)
        else:
            return "{0}'s Profile".format(self.username)

我对

__str __
进行了如下测试:

from django.test import TestCase

from accounts.models import User
from common.tests.fixtureless import create_instance


class UserTest(TestCase):
    def test_str(self):
        initial = {
            'username': 'test_username',
            'first_name': 'test_first_name',
            'last_name': 'test_last_name',
        }
        user = create_instance(User, **initial)
        user.save()
        expected = "test_first_name test_last_name's Profile"
        self.assertEqual(user.__str__(), expected)
        user.delete()

        del(initial['last_name'])
        user = create_instance(User, **initial)
        user.save()
        expected = "test_first_name's Profile"
        self.assertEqual(user.__str__(), expected)
        user.delete()

        del(initial['first_name'])
        user = create_instance(User, **initial)
        user.save()
        expected = "test_username's Profile"
        self.assertEqual(user.__str__(), expected)
        user.delete()

我可以在大约 20% 的时间内正确运行我的测试套件。然而,有时我会收到以下失败消息:

ERROR: test_str (blind_tiger.blind_tiger.apps.accounts.tests.UserTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/ricomoss/.virtualenvs/blind_tiger/lib/python3.3/site-packages/django/db/backends/util.py", line 53, in execute
    return self.cursor.execute(sql, params)
psycopg2.ProgrammingError: relation "accounts_user" does not exist
LINE 1: INSERT INTO "accounts_user" ("password", "last_login", "is_s...
                    ^


The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/ricomoss/workspace/ses/blind_tiger/blind_tiger/settings/../apps/accounts/tests/models.py", line 18, in test_str
    user.save()
  File "/home/ricomoss/.virtualenvs/blind_tiger/lib/python3.3/site-packages/django/db/models/base.py", line 545, in save
    force_update=force_update, update_fields=update_fields)
  File "/home/ricomoss/.virtualenvs/blind_tiger/lib/python3.3/site-packages/django/db/models/base.py", line 573, in save_base
    updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
  File "/home/ricomoss/.virtualenvs/blind_tiger/lib/python3.3/site-packages/django/db/models/base.py", line 654, in _save_table
    result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
  File "/home/ricomoss/.virtualenvs/blind_tiger/lib/python3.3/site-packages/django/db/models/base.py", line 687, in _do_insert
    using=using, raw=raw)
  File "/home/ricomoss/.virtualenvs/blind_tiger/lib/python3.3/site-packages/django/db/models/manager.py", line 232, in _insert
    return insert_query(self.model, objs, fields, **kwargs)
  File "/home/ricomoss/.virtualenvs/blind_tiger/lib/python3.3/site-packages/django/db/models/query.py", line 1511, in insert_query
    return query.get_compiler(using=using).execute_sql(return_id)
  File "/home/ricomoss/.virtualenvs/blind_tiger/lib/python3.3/site-packages/django/db/models/sql/compiler.py", line 898, in execute_sql
    cursor.execute(sql, params)
  File "/home/ricomoss/.virtualenvs/blind_tiger/lib/python3.3/site-packages/django/db/backends/util.py", line 53, in execute
    return self.cursor.execute(sql, params)
  File "/home/ricomoss/.virtualenvs/blind_tiger/lib/python3.3/site-packages/django/db/utils.py", line 99, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/home/ricomoss/.virtualenvs/blind_tiger/lib/python3.3/site-packages/django/utils/six.py", line 490, in reraise
    raise value.with_traceback(tb)
  File "/home/ricomoss/.virtualenvs/blind_tiger/lib/python3.3/site-packages/django/db/backends/util.py", line 53, in execute
    return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: relation "accounts_user" does not exist
LINE 1: INSERT INTO "accounts_user" ("password", "last_login", "is_s...

这令人沮丧,因为如果我在有问题的

try/except
周围放置
user.save()
,这样我就可以检查该对象,我永远不会看到此错误。

永远不会产生此错误:

try:
    user.save()
except:
    raise Exception(user.__dict__)

有什么建议吗?

python django unit-testing django-models django-testing
3个回答
6
投票

尝试运行以下命令:

python manage.py migrate --run-syncdb
python manage.py makemigrations appname
python manage.py migrate

4
投票

我解决了这个问题。先尝试运行一下:

python manage.py syncdb

0
投票

如果此错误发生在 docker 中 要在“Windows”或“Ubuntu”上删除 Docker 中的卷,可以使用 docker volume rm 命令。按着这些次序: 打开命令提示符或 PowerShell 窗口。 使用以下命令列出所有现有卷:

docker volume ls

这将显示卷列表及其名称。 确定要删除的卷并复制其名称。 使用以下命令删除指定卷:

docker volume rm <volume_name>

替换为要删除的卷的实际名称。 例如:

docker volume rm my_volume

此命令将删除指定的卷。

请记住,如果仍有容器在使用某个卷,则无法删除该卷。确保当前没有容器正在使用要删除的卷。如果有,您可能需要先停止或删除这些容器。

此外,如果卷作为绑定挂载安装在容器中,请确保在尝试删除卷之前容器未运行。

© www.soinside.com 2019 - 2024. All rights reserved.