#31730: manage.py sqlsequencereset not implemented for sqlite3
-------------------------------------+-------------------------------------
     Reporter:  axil                 |                    Owner:  nobody
         Type:  Bug                  |                   Status:  new
    Component:  Database layer       |                  Version:  3.0
  (models, ORM)                      |
     Severity:  Normal               |               Resolution:
     Keywords:  sql sequence reset   |             Triage Stage:
  sqlite3                            |  Unreviewed
    Has patch:  1                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------
Description changed by axil:

Old description:

> 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)
> }}}

New description:

 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.

 Leniency of sqlite is also the explanation of how
 `django/tests/backends/tests.py:SequenceResetTest.test_generic_relation`
 test contrives to pass with `sequence_reset_sql` being noop for sqlite3.

 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#comment:3>
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/062.52e3a728e050442dce208e619b1d395f%40djangoproject.com.

Reply via email to