#31730: manage.py sqlsequencereset not implemented for sqlite3
-------------------------------------+-------------------------------------
               Reporter:  axil       |          Owner:  nobody
                   Type:  Bug        |         Status:  new
              Component:  Database   |        Version:  3.0
  layer (models, ORM)                |       Keywords:  sql sequence reset
               Severity:  Normal     |  sqlite3
           Triage Stage:             |      Has patch:  1
  Unreviewed                         |
    Needs documentation:  0          |    Needs tests:  0
Patch needs improvement:  0          |  Easy pickings:  0
                  UI/UX:  0          |
-------------------------------------+-------------------------------------
 Currently `./manage.py sqlsequencereset`
 ([https://docs.djangoproject.com/en/3.0/ref/django-admin/#sqlsequencereset
 docs]) never prints anything for the sqlite3 backend.

 It happens because the function `sequence_reset_sql` defined in
 `django/django/db/backends/base/operations.py`
 is not implemented for the sqlite3 backend in
 `django/django/db/backends/sqlite3/operations.py`

 `django/django/contrib/sites/management.py` makes use of this function

 {{{
     # We set an explicit pk instead of relying on auto-incrementation,
     # so we need to reset the database sequence. See #17415.
     sequence_sql = connections[using].ops.sequence_reset_sql(no_style(),
 [Site])
     if sequence_sql:
         if verbosity >= 2:
              print("Resetting sequence")
 }}}

 Here `sequence_reset_sql` also does nothing for sqlite3, but sqlite3 is
 pretty lenient to
 the sequences, so it didn't result in an exception.

 Same applies to its usage in `manage.py loaddata`: sqlite3 fixes the
 sequences automatically.

 What it cannot do automatically is reset the sequence to zero as
 demonstrated in the new test
 `django/tests/backends/tests.py:SequenceResetTest.test_reset_sequence`

 {{{
     def test_reset_sequence(self):
         Post.objects.create(name='1st post', text='hello world')

         Post.objects.all().delete()

         # Reset the sequences for the database
         commands =
 connections[DEFAULT_DB_ALIAS].ops.sequence_reset_sql(no_style(), [Post])
         with connection.cursor() as cursor:
             for sql in commands:
                 cursor.execute(sql)

         # If we create a new object now, it should have a PK greater
         # than the PK we specified manually.
         obj = Post.objects.create(name='New post', text='goodbye world')
         self.assertEqual(obj.pk, 1)
 }}}

-- 
Ticket URL: <https://code.djangoproject.com/ticket/31730>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/047.2d5a6745ad940194610ddd873d59b97a%40djangoproject.com.

Reply via email to