如何使用 migrate appname --fake 运行测试?

1 投票
1 回答
578 浏览
提问于 2025-04-17 15:45

我的测试不工作。如果我尝试运行 python manage.py test appname,就会出现这个错误:

! You *might* be able to recover with:   = DROP TABLE "appname_userprofile"; []
   = DROP TABLE "appname_table2"; []
   = DROP TABLE "appname_table3"; []

 ! The South developers regret this has happened, and would
 ! like to gently persuade you to consider a slightly
 ! easier-to-deal-with DBMS (one that supports DDL transactions)
 ! NOTE: The error which caused the migration to fail is further up.
Error in migration: content:0015_initial
django.db.utils.DatabaseError: table "appname_userprofile" already exists

我该如何运行我的 python manage.py test appname 呢?

manage.py migrate appname --fake

1 个回答

3

根据我的了解,你需要写一个自定义的测试运行器,来选择性地给每个迁移添加 --fake 这个选项。

你可能应该先修复你的数据库迁移,因为看起来你有两个迁移在尝试创建同一个表。

South 在开始测试之前,想要按顺序运行你所有的迁移,以建立一个初始的数据库,但现在它做不到这一点。

如果你想在单元测试中完全禁用 South,可以在你的 settings.py 文件中加入这个设置(参考链接):

SOUTH_TESTS_MIGRATE = False

这样一来,Django 的测试运行器就会根据你当前的模型直接创建测试数据库,而不是通过迁移来构建它。

撰写回答