Ticket #8739: save_force_insert.diff

File save_force_insert.diff, 1.2 KB (added by Richard Davies <richard.davies@…>, 7 years ago)

Extra test case (note that the init.py must be created too)

  • tests/regressiontests/save_force_insert/models.py

     
     1from django.db import models, IntegrityError
     2
     3class ManualPrimaryKeyTest(models.Model):
     4    id = models.IntegerField(primary_key=True)
     5    data = models.CharField(max_length=100)
     6
     7__test__ = {'API_TESTS':"""
     8# If you force_insert specifying an existing primary key, but different
     9# other fields, then you will get an error and data will not be updated.
     10>>> m = ManualPrimaryKeyTest(id=1, data='Original')
     11>>> m.save(force_insert=True)
     12>>> try:
     13...    m = ManualPrimaryKeyTest(id=1, data='Different')
     14...    m.save(force_insert=True)
     15... except Exception, e:
     16...    if isinstance(e, IntegrityError):
     17...        print "Pass"
     18...    else:
     19...        print "Fail with %s" % type(e)
     20Pass
     21>>> ManualPrimaryKeyTest.objects.get(id=1).data == 'Original'
     22True
     23"""}
Back to Top