Django测试错误:关系不存在

2024-05-19 18:49:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我在Ubuntu13.10和Postgres上使用Django1.6和Python3.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 __的测试,如下所示:

^{pr2}$

我可以正常运行我的测试套件大约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__)

有什么建议吗?在


Tags: djangoinpyselfhomedblibpackages