【问题标题】:Django test can't initialize db but I canDjango 测试无法初始化数据库,但我可以
【发布时间】:2013-07-16 10:14:02
【问题描述】:

标题不清楚,我知道。
我想对我的应用程序进行单元测试,所以我写了一个我想执行的小测试。我启动了python manage.py test,但数据库中有错误:

The error was: ERREUR:  the relation « me_auth_emailuser » doesn't exists

Error in migration: authtoken:0001_initial
DatabaseError: ERREUR:  the relation « me_auth_emailuser » doesn't exists

(从法语翻译)
此表已使用南迁移。对于我的应用程序,我只使用:

python manage.py syncdb
python manage.py migrate me_auth
python manage.py migrate

我不明白发生了什么,因为使用这些命令我没有收到任何错误...有人可以帮助我吗? :)

【问题讨论】:

    标签: database django testing


    【解决方案1】:

    大概在某个时候你有一个关系me_auth_email_user,你不再拥有了。我想如果你要创建一个新的数据库并运行,你会得到同样的错误:

    python manage.py syncdb
    python manage.py migrate
    

    有两种解决方案:

    1. 不要在您的单元测试中使用 South(如果您正在测试,请从您的 INSTALLED_APPS 中删除它,如下所示,或者在您的 settings.py 中设置 SOUTH_TESTS_MIGRATE = False)。
    2. 手动修复损坏的迁移。

    在测试期间删除 South 的一种快速而简单的方法是在您的 settings.py 中添加类似的内容,低于您的正常 INSTALLED_APPS 设置:

    import sys
    
    if 'test' in sys.argv:
      INSTALLED_APPS = [app for app in INSTALLED_APPS if app != 'south']
    

    一般来说,测试迁移是一件好事 - 您应该始终能够创建一个新数据库并运行 migrate - 所以我强烈建议考虑选项 (2)。

    【讨论】:

      猜你喜欢
      • 2015-12-17
      • 2019-02-20
      • 2013-08-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多