Django:执行manage.py测试myApp时,关系“auth_user”已经存在

时间:2014-01-17 06:15:21

标签: python-2.7 django-models postgresql-9.2 django-unittest

堆栈跟踪:

Creating test database for alias 'default'...
Destroying old test database 'default'...
Traceback (most recent call last):

File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "C:\Py27_64\lib\site-packages\django\core\management\__init__.py", line 399, in execute_from_command_line
utility.execute()
File "C:\Py27_64\lib\site-packages\django\core\management\__init__.py", line 392, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\Py27_64\lib\site-packages\django\core\management\commands\test.py", line 50, in run_from_argv
super(Command, self).run_from_argv(argv)
File "C:\Py27_64\lib\site-packages\django\core\management\base.py", line 242, in run_from_argv
self.execute(*args, **options.__dict__)
File "C:\Py27_64\lib\site-packages\django\core\management\commands\test.py", line 71, in execute
super(Command, self).execute(*args, **options)
File "C:\Py27_64\lib\site-packages\django\core\management\base.py", line 285, in execute
output = self.handle(*args, **options)
File "C:\Py27_64\lib\site-packages\django\core\management\commands\test.py", line 88, in handle
failures = test_runner.run_tests(test_labels)
File "C:\Py27_64\lib\site-packages\django\test\runner.py", line 145, in run_tests
old_config = self.setup_databases()
File "C:\Py27_64\lib\site-packages\django\test\runner.py", line 107, in setup_databases
return setup_databases(self.verbosity, self.interactive, **kwargs)
File "C:\Py27_64\lib\site-packages\django\test\runner.py", line 279, in setup_databases
verbosity, autoclobber=not interactive)
File "C:\Py27_64\lib\site-packages\django\db\backends\creation.py", line 339, in create_test_db
load_initial_data=False)
File "C:\Py27_64\lib\site-packages\django\core\management\__init__.py", line 159, in call_command
return klass.execute(*args, **defaults)
File "C:\Py27_64\lib\site-packages\django\core\management\base.py", line 285, in execute
output = self.handle(*args, **options)
File "C:\Py27_64\lib\site-packages\django\core\management\base.py", line 415, in handle
return self.handle_noargs(**options)
File "C:\Py27_64\lib\site-packages\django\core\management\commands\syncdb.py", line 107, in handle_noargs
cursor.execute(statement)
File "C:\Py27_64\lib\site-packages\django\db\backends\util.py", line 53, in execute
return self.cursor.execute(sql, params)
File "C:\Py27_64\lib\site-packages\django\db\utils.py", line 99, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "C:\Py27_64\lib\site-packages\django\db\backends\util.py", line 51, in execute
return self.cursor.execute(sql)
django.db.utils.ProgrammingError: relation "auth_user" already exists

我的settings.py文件:

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.admin',
            'myApp',
)

我的怀疑:在models.py中我手动添加了类AuthUser(),原因是之前对models.py的实际工作方式存在误解。

我想要自定义字段/属性,所以我认为这是实现它的方法。另外,我实际上修改了由Django生成的postgres中的auth_user表来添加自定义字段。

现在,当我尝试运行./manage.py测试myApp时,我得到了上面的堆栈跟踪。下面是类AuthUser():

class AuthUser(models.Model):
    password = models.CharField(max_length=128, blank=True)
    username = models.CharField(max_length=30, blank=True)
    first_name = models.CharField(max_length=30, blank=True)
    last_name = models.CharField(max_length=30, blank=True)
    is_staff = models.BooleanField(default=True)
    is_active = models.BooleanField(default=True)
    email = models.CharField(max_length=75, blank=True)
    uuid = models.Uuid()
    role = models.CharField(max_length=20, blank=True)
    curricula = models.ManyToManyField(Curricula,
            through='CurriculaUserMap',
            null=True, 
            blank=True)

    class Meta:
        db_table = 'auth_user'
        get_latest_by = 'id'

    def __unicode__(self):
         return self.username

我还没有验证上面的堆栈跟踪是由于在执行./manage.py test myApp的上下文中的models.py中定义类AuthUser()而引起的。我的问题是:

  • 如何继续当前实现models.py并成功执行./manage.py test myApp并继续进行单元测试?
  • 如何在models.py中使用自定义字段修复AuthUser类的正确方法?
    • 我可以通过将一对一关系的Django的User类扩展到models.py中的CustomUser类来解决这个问题。
    • 它是否也支持我在Postgres DB中创建的与CurriculaUserMap的ManyToManyField关系?

我担心的是,如果我跳起来解决这个“正确的方式”;它几乎可以打破一切。

谢谢你的时间!

1 个答案:

答案 0 :(得分:0)

所以我回答了自己的问题:

  • 没有别的方法可以在models.py中创建一个AuthUser类。它必须被删除,并且在我的views.py中的任何地方,我引用的AuthUser必须更新以指向Django内置的User对象。
  • 在models.py中创建一个CustomUser类,并将一个属性定义为models.OneToOneField到Django内置的User对象比预期的要容易。
    • 关于“多对多”字段,我只是将其移到了“多对多”映射的另一个“结尾”;它不必在CustomUser对象中定义。