#35763: Setting the auto increment value of a database model breaks when you 
add a
new field to the model
-------------------------------------+-------------------------------------
     Reporter:  Kevin Renskers       |                     Type:  Bug
       Status:  new                  |                Component:  Database
                                     |  layer (models, ORM)
      Version:  5.1                  |                 Severity:  Normal
     Keywords:                       |             Triage Stage:
                                     |  Unreviewed
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------
 Setting the auto increment value of a database model breaks when you add a
 new field to the model. It only breaks in SQLite.

 I have simple model `Content`:

 {{{
 class Content(models.Model):
     name = models.CharField(max_length=255, blank=True)
 }}}

 In the initial migration I've added an extra operation:

 {{{
 migrations.RunSQL("INSERT INTO sqlite_sequence (seq, name) VALUES (50000,
 'content_content');"),
 }}}

 Now when I create my first model instance its ID starts at 50,001, as
 expected. However when I then add a second field to my `Content` model and
 create a migration, now my first model instance ID is 1 instead of 50,001.
 Somehow the `migrations.AddField` operation has undone the
 `sqlite_sequence` modification.

 I have a repro here: https://github.com/kevinrenskers/django-seq-repro.

 It has two commits:

 1. The initial commit where a `Content` model is added, and in the initial
 migration I set the initial auto increment value. A test is added to make
 sure the first model instance has ID 50,001, which passes.
 2. The second commit adds a single field to the `Content` model (with the
 accompanying migration file), and now the test fails.

 The test can be fixed by adding another `RunSQL` command to the second
 migration:
 `migrations.RunSQL("UPDATE sqlite_sequence SET seq = 50000 WHERE name =
 'content_content'"),`

 It seems like a bug that the auto increment modification is undone by the
 second migration.

 Important to note: this only happens in SQLite. With PostgreSQL the query
 in the initial migration would be `ALTER SEQUENCE content_content_id_seq
 RESTART WITH 50000;`, and the test still passes after adding the new field
 to the `Content` model. Only with SQLite does it break and is the second
 operation to update `sqlite_sequence` necessary.
-- 
Ticket URL: <https://code.djangoproject.com/ticket/35763>
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/01070191f6c45bd9-8a0338bc-750c-465e-a439-a27714ffb9de-000000%40eu-central-1.amazonses.com.

Reply via email to