Django没有正确创建测试数据库

[英]Django is not creating the test database correctly


I am using the following database settings

我使用以下数据库设置

DATABASES = {
    'default': {
         'ENGINE': 'django.db.backends.sqlite3',
         'NAME': os.path.join(_DB_DIR, 'dev.db'),
         'TEST_NAME': os.path.join(_DB_DIR, 'dev_test.db'),
    }
}

However, every time I run python manage.py test it breaks. The problem seems to be on the test database. For some reason the same tables from the standard db are not being created over there. The command returns django.db.utils.DatabaseError: no such table: tbforms_userprofile . And indeed, when I open the dev_test.db using sqlite3 it's possible to see the specified table (and any other from the tbforms application) does not exist.

但是,每次运行python manage.py测试时都会中断。问题似乎是在测试数据库上。由于某种原因,那里没有创建标准数据库中的相同表。该命令返回django.db.utils.DatabaseError:没有这样的表:tbforms_userprofile。事实上,当我使用sqlite3打开dev_test.db时,可能会看到指定的表(以及tbforms应用程序中的任何其他表)不存在。

What am I missing? I'm using Django 1.4 with Sqlite

我错过了什么?我正在使用带有Sqlite的Django 1.4

PS: Important to notice that syncdb and migrate run smoothly. The complete traceback is available here: http://pastebin.com/9dVmuVyt

PS:重要的是要注意syncdb和迁移运行顺利。完整的回溯可在此处获得:http://pastebin.com/9dVmuVyt

1 个解决方案

#1


1  

As we've discussed, the issue here is that there's a post save signal for the User model (which is created during syncdb) to create a Profile model (which is created after syncdb, at south's migrate). So, when syncdb (or your tests) creates the tables on a new database and creates a new user, the table for the user profile is not yet created and this error is raised.

正如我们已经讨论过的,这里的问题是用户模型(在syncdb期间创建)有一个保存后信号来创建一个Profile模型(在南方迁移后的syncdb之后创建)。因此,当syncdb(或您的测试)在新数据库上创建表并创建新用户时,尚未创建用户配置文件的表,并引发此错误。

Solutions:

  • Avoid the creation of this profile automatically at User creation (bad smells about it)
  • 避免在创建用户时自动创建此配置文件(关于它的味道很难闻)

  • Use Django 1.5 and it's custom user model support
  • 使用Django 1.5及其自定义用户模型支持

  • Disabling south for tests (SOUTH_TESTS_MIGRATE = False) and always creating new databases specifying syncdb --all then migrate --fake. (not the best solution, though)
  • 禁用南以进行测试(SOUTH_TESTS_MIGRATE = False)并始终创建指定syncdb的新数据库 - 然后再迁移--fake。 (虽然不是最好的解决方案)


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.silva-art.net/blog/2013/08/05/81e6f8d0f5d8918b15ffd3481f68bf62.html



 
© 2014-2018 ITdaan.com 粤ICP备14056181号