Django models django模型测试中的事务管理错误
在django 1.6中,我尝试测试一个唯一的字段Django models django模型测试中的事务管理错误,django-models,django-testing,django-1.6,Django Models,Django Testing,Django 1.6,在django 1.6中,我尝试测试一个唯一的字段 # model tag class Tag(models.Model): name = models.CharField(max_length=30, unique=True, null=True) def __unicode__(self): return self.name # test unique of name field class TagTest(TestCase): def tes
# model tag
class Tag(models.Model):
name = models.CharField(max_length=30, unique=True, null=True)
def __unicode__(self):
return self.name
# test unique of name field
class TagTest(TestCase):
def test_tag_unique(self):
t1 = Tag(name='music')
t1.save()
with self.assertRaises(IntegrityError):
t2 = Tag(name='music')
t2.save()
self.assertEqual(['music'], [ t.name for t in Tag.objects.all() ])
最后一行我收到了这个信息
"An error occurred in the current transaction. You can't "
TransactionManagementError: An error occurred in the current transaction. You can't execute queries until the end of the 'atomic' block.
为什么?
编辑
我将sqlite作为DB(开发环境)来实现这一点。如果您使用的是PostgreSQL,那么这就是原因 编辑: 见提交。因为它位于基本后端,所以似乎所有后端现在都有相同的行为。尽管使用了后端,但如果事务需要回滚,则会引发错误 提示:
使用
Model.objects.create(attr=“value”)
而不是create和.save()
我在尝试使用sqlite进行测试时得到了这个结果。