Django reset_sequences在LiveServerTestCase中不起作用 [英] Django reset_sequences doesn't work in LiveServerTestCase
问题描述
我已经将Django从1.6更新到1.8.3.我在单元测试的test setUp方法中创建测试模型,诸如此类
I've updated django from 1.6 to 1.8.3. I create test models in test setUp method in unit tests, something like that
class MyTestCase(LiveServerTestCase):
reset_sequences = True
def setUp(self):
self.my_model = models.MyModel.objects.create(name='test)
我在应用程序中有依赖主键== 1的代码.我注意到,实际上并没有重置序列.在每个下一个测试中,pk都大于前一个.
And I have code in the application, which relies on the primary key == 1. I've noted, that sequences aren't actually reseted. In each next test the pk is greater, that in previous.
在django 1.6中可以正常工作,但是在移植到1.8版本后会出现问题.
This works ok in django 1.6, but after migration to 1.8 problems appears.
我应该手动重置序列吗?
Should I reset sequence manually?
P.s.我了解固定装置,但是我的模型更复杂,对我来说,在代码中创建模型更容易.
P.s. I know about fixtures, but my models are more complicated and for me it's easier to create models in the code.
推荐答案
问题出在sqlite3中.这些测试已与其他设置文件一起运行,其中sqlite3配置为数据库.
The problem was in sqlite3. The tests have been run with other settings file, where sqlite3 is configured as database.
django检查数据库是否支持序列:
The django checks, if the database supports sequences:
# django.test.testcases.py:809
def _reset_sequences(self, db_name):
conn = connections[db_name]
if conn.features.supports_sequence_reset:
sql_list = conn.ops.sequence_reset_by_name_sql(
no_style(), conn.introspection.sequence_list())
# ....
所以我将测试设置切换到了PostgreSQL,现在它可以正常工作了
So I've switched test settings to the postgresql and now it works normally
这篇关于Django reset_sequences在LiveServerTestCase中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!