Django:执行manage.py test myApp时,关系“auth_user”已经存在
Posted
技术标签:
【中文标题】Django:执行manage.py test myApp时,关系“auth_user”已经存在【英文标题】:Django: relation "auth_user" already exists when executing manage.py test myApp 【发布时间】:2014-01-17 06:15:21 【问题描述】:堆栈跟踪:
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 中,由于之前对 models.py 实际工作方式的误解,我手动添加了类 AuthUser()。
我想要自定义字段/属性,所以我认为这是实现它的方法。此外,我实际上修改了 Django 生成的 postgres 中的 auth_user 表以添加自定义字段。
现在,当我尝试运行 ./manage.py test 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() 引起的。我的问题是:
./manage.py test myApp
并继续我的单元测试?
如何在 models.py 中使用自定义字段修复 AuthUser 类的正确方法?
我能否通过将 Django 的 User 类与 models.py 中的 CustomUser 类以一对一的关系扩展来解决此问题
它是否也支持我在 postgres DB 中创建的与 CurriculaUserMap 的 ManyToManyField 关系?
我担心的是,如果我采取“正确的方式”来解决这个问题;它几乎会破坏一切。
感谢您的宝贵时间!
【问题讨论】:
【参考方案1】:所以我回答了我自己的问题:
没有其他方法可以解决在您的 models.py 中创建 AuthUser 类不会成功的事实。它必须被删除,并且在我的 views.py 中我引用 AuthUser 的任何地方都必须更新以指向 Django 内置的 User 对象。 在 models.py 中创建 CustomUser 类并将属性定义为 models.OneToOneField 到 Django 内置 User 对象比预期的要容易。 关于多对多字段,我只是将其移至多对多映射的另一个“端”;它不必在 CustomUser 对象中定义。【讨论】:
以上是关于Django:执行manage.py test myApp时,关系“auth_user”已经存在的主要内容,如果未能解决你的问题,请参考以下文章
Django 2.0教程 - 执行python3 manage.py makemigrations时出错
如何直接从测试驱动程序调用自定义 Django manage.py 命令?
Py.test 失败,但 ./manage.py 测试工作正常