#27558: Setting db_index=False on existing ForeignKey causes constraint to be
recreated
-------------------------------------+-------------------------------------
               Reporter:  Ed Morley  |          Owner:  nobody
                   Type:  Bug        |         Status:  new
              Component:  Database   |        Version:  1.10
  layer (models, ORM)                |
               Severity:  Normal     |       Keywords:  db-indexes, mysql
           Triage Stage:             |      Has patch:  0
  Unreviewed                         |
    Needs documentation:  0          |    Needs tests:  0
Patch needs improvement:  0          |  Easy pickings:  0
                  UI/UX:  0          |
-------------------------------------+-------------------------------------
 Using:
 * Django 1.10.3
 * MySQL 5.6

 STR:
 1) Start with this models.py:
 {{{#!python
 class Musician(models.Model):
     first_name = models.CharField(max_length=50)
     last_name = models.CharField(max_length=50)


 class Album(models.Model):
     artist = models.ForeignKey(Musician)
     name = models.CharField(max_length=100)

     class Meta:
         unique_together = ('artist', 'name')
 }}}

 2) Run `./manage.py makemigrations && ./manage.py migrate` to generate and
 apply the initial migration.
 3) Add `db_index=False` to the `ForeignKey` (to work around the duplicate
 index issue discussed [https://groups.google.com/forum/#!topic/django-
 developers/3ywugkcaxqs here]) - making it:
 {{{#!python
     artist = models.ForeignKey(Musician, db_index=False)
 }}}
 4) Run `./manage.py makemigrations`
 5) Run `./manage.py sqlmigrate <foo> 0002` to display the generated SQL

 Expected:
 Just the index is dropped.

 {{{#!sql
 BEGIN;
 --
 -- Alter field artist on album
 --
 DROP INDEX `foo_album_ca949605` ON `foo_album`;
 COMMIT;
 }}}

 Actual:

 The constraint is removed and re-added, which is time consuming since the
 constraint is an index in itself.

 {{{#!sql
 BEGIN;
 --
 -- Alter field artist on album
 --
 ALTER TABLE `foo_album` DROP FOREIGN KEY
 `foo_album_artist_id_66b4953c_fk_foo_musician_id`;
 DROP INDEX `foo_album_ca949605` ON `foo_album`;
 ALTER TABLE `foo_album` ADD CONSTRAINT
 `foo_album_artist_id_66b4953c_fk_foo_musician_id` FOREIGN KEY
 (`artist_id`) REFERENCES `foo_musician` (`id`);
 COMMIT;
 }}}

 Additional context:
 * The explicit `db_index=False` is needed to work around the duplicate
 index issue discussed [https://groups.google.com/forum/#!topic/django-
 developers/3ywugkcaxqs here].
 * The duplicate index issue appears to be fixed on master (not sure by
 what) causing this not to repro there.
 * I'm presuming the duplicate index isn't severe enough to warrant
 backporting, however dropping and re-adding the constraint seems pretty
 bad, so perhaps more worthy of a backport to 1.10.x?

--
Ticket URL: <https://code.djangoproject.com/ticket/27558>
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 post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/051.6576ba91863e1b8292ce44df44aa2610%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to