Django测试期间出错

时间:2016-04-11 16:08:13

标签: python django unit-testing

我试图在Django中运行测试,当Django尝试创建测试数据库时,我不断收到此错误。

django.db.utils.ProgrammingError: relation "userprofile_user" does not exist

这是我的UserProfile模型:

class UserProfile(models.Model):
    user = models.OneToOneField(User, related_name='profile')
    gender = models.CharField(max_length=140, null=True)
    age_range_min = models.PositiveIntegerField(null=True)
    age_range_max = models.PositiveIntegerField(null=True)
    locale = models.CharField(max_length=255, null=True)

    def __unicode__(self):
        return u'Profile of user: %s' % self.user.email

这是完整的堆栈跟踪:

Destroying old test database 'default'...
Traceback (most recent call last):
  File "manage.py", line 11, in <module>
    execute_from_command_line(sys.argv)
  File "/Users/chad/.virtualenvs/site/lib/python2.7/site-packages/django/core/management/__init__.py", line 354, in execute_from_command_line
    utility.execute()
  File "/Users/chad/.virtualenvs/site/lib/python2.7/site-packages/django/core/management/__init__.py", line 346, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/Users/chad/.virtualenvs/site/lib/python2.7/site-packages/django/core/management/commands/test.py", line 30, in run_from_argv
    super(Command, self).run_from_argv(argv)
  File "/Users/chad/.virtualenvs/site/lib/python2.7/site-packages/django/core/management/base.py", line 394, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/Users/chad/.virtualenvs/site/lib/python2.7/site-packages/django/core/management/commands/test.py", line 74, in execute
    super(Command, self).execute(*args, **options)
  File "/Users/chad/.virtualenvs/site/lib/python2.7/site-packages/django/core/management/base.py", line 445, in execute
    output = self.handle(*args, **options)
  File "/Users/chad/.virtualenvs/site/lib/python2.7/site-packages/django/core/management/commands/test.py", line 90, in handle
    failures = test_runner.run_tests(test_labels)
  File "/Users/chad/.virtualenvs/site/lib/python2.7/site-packages/django/test/runner.py", line 210, in run_tests
    old_config = self.setup_databases()
  File "/Users/chad/.virtualenvs/site/lib/python2.7/site-packages/django/test/runner.py", line 166, in setup_databases
    **kwargs
  File "/Users/chad/.virtualenvs/site/lib/python2.7/site-packages/django/test/runner.py", line 370, in setup_databases
    serialize=connection.settings_dict.get("TEST", {}).get("SERIALIZE", True),
  File "/Users/chad/.virtualenvs/site/lib/python2.7/site-packages/django/db/backends/base/creation.py", line 368, in create_test_db
    test_flush=not keepdb,
  File "/Users/chad/.virtualenvs/site/lib/python2.7/site-packages/django/core/management/__init__.py", line 120, in call_command
    return command.execute(*args, **defaults)
  File "/Users/chad/.virtualenvs/site/lib/python2.7/site-packages/django/core/management/base.py", line 445, in execute
    output = self.handle(*args, **options)
  File "/Users/chad/.virtualenvs/site/lib/python2.7/site-packages/django/core/management/commands/migrate.py", line 179, in handle
    created_models = self.sync_apps(connection, executor.loader.unmigrated_apps)
  File "/Users/chad/.virtualenvs/site/lib/python2.7/site-packages/django/core/management/commands/migrate.py", line 318, in sync_apps
    cursor.execute(statement)
  File "/Users/chad/.virtualenvs/site/lib/python2.7/site-packages/django/db/backends/utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
  File "/Users/chad/.virtualenvs/site/lib/python2.7/site-packages/django/db/utils.py", line 98, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/Users/chad/.virtualenvs/site/lib/python2.7/site-packages/django/db/backends/utils.py", line 62, in execute
    return self.cursor.execute(sql)
django.db.utils.ProgrammingError: relation "userprofile_user" does not exist

1 个答案:

答案 0 :(得分:1)

您的迁移似乎存在问题,请尝试将其添加到您的设置中,以便在测试期间传递它们:

p = N/2