Opened 6 years ago

Closed 6 years ago

#29559 closed Bug (fixed)

TransactionTestCase.reset_sequences doesn't reset sequences of auto created ManyToMany models

Reported by: Oliver Sauder Owned by: Oliver Sauder
Component: Testing framework Version: dev
Severity: Normal Keywords:
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Consider following example:

class Person(models.Model):
    first_name = models.CharField(max_length=20)
    last_name = models.CharField(max_length=20)

    friends = models.ManyToManyField('self')

class AutoIncrementResetTest(TransactionTestCase):
    reset_sequences = True

    def test_autoincrement_reset1(self):
        p = Person.objects.create(first_name='Jack', last_name='Smith')
        self.assertEqual(p.pk, 1)

        p.friends.add(Person.objects.create(first_name='Jacky', last_name='Smith'))
        self.assertEqual(p.friends.through.objects.first().pk, 1)

    def test_autoincrement_reset2(self):
        p = Person.objects.create(first_name='Jack', last_name='Smith')
        self.assertEqual(p.pk, 1)

        p.friends.add(Person.objects.create(first_name='Jacky', last_name='Smith'))
        self.assertEqual(p.friends.through.objects.first().pk, 1)

Test test_autoincrement_reset2 will fail with a much higher primary key, indicating that sequence has not been properly reset. Tested this on a Postgres database but I assume this error exist on other databases as well.

Change History (4)

comment:1 by Oliver Sauder, 6 years ago

Owner: changed from nobody to Oliver Sauder
Status: newassigned

comment:2 by Oliver Sauder, 6 years ago

Has patch: set

comment:3 by Tim Graham, 6 years ago

Triage Stage: UnreviewedReady for checkin

comment:4 by Tim Graham <timograham@…>, 6 years ago

Resolution: fixed
Status: assignedclosed

In a07a49ee:

Fixed #29559 -- Fixed TransactionTestCase.reset_sequences for auto-created m2m through models.

Note: See TracTickets for help on using tickets.
Back to Top