#28803: When renaming foreign key indexes don't get renamed which breaks next
migrations
-----------------------------------------+------------------------
               Reporter:  Jacek Bzdak    |          Owner:  nobody
                   Type:  Uncategorized  |         Status:  new
              Component:  Migrations     |        Version:  1.11
               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              |
-----------------------------------------+------------------------
 Here is what I did. I have following models:

 {{{
 class ReferencedModel(models.Model):
    pass


 class SecondReferencedModel(models.Model):
    pass


 class ReferenceeModel(models.Model):
   child = models.ForeignKey(ReferencedModel, default=None,
 on_delete=models.CASCADE)
 }}}

 I want to swichch ``ReferenceeModel.child`` to ``SecondReferencedModel``.

 I want to do it by:

 1) Renaming ``ReferenceeModel.child_old``
 2) Adding ``ReferenceeModel.child`` that points to proper model
 3) Some RunPython magic
 4) Delete  ``ReferenceeModel.child_old``

 Actual bug:

 {{{

 class Migration(migrations.Migration):

     initial = True

     dependencies = [
     ]

     operations = [
         migrations.CreateModel(
             name='ReferencedModel',
             fields=[
                 ('id', models.AutoField(auto_created=True,
 primary_key=True, serialize=False, verbose_name='ID')),
             ],
         ),
         migrations.CreateModel(
             name='SecondReferencedModel',
             fields=[
                 ('id', models.AutoField(auto_created=True,
 primary_key=True, serialize=False, verbose_name='ID')),
             ],
         ),
         migrations.CreateModel(
             name='ReferenceeModel',
             fields=[
                 ('id', models.AutoField(auto_created=True,
 primary_key=True, serialize=False, verbose_name='ID')),
                 ('child',
 models.ForeignKey(on_delete=django.db.models.deletion.CASCADE,
 to='index_error.ReferencedModel')),
             ],
         ),
     ]

 class Migration(migrations.Migration):

     dependencies = [
         ('index_error', '0001_initial'),
     ]

     operations = [
         migrations.RenameField(
             model_name='referenceemodel',
             old_name='child',
             new_name='child_copy',
         ),
     ]


 class Migration(migrations.Migration):

     dependencies = [
         ('index_error', '0002_auto_20171116_1505'),
     ]

     operations = [
         migrations.AddField(
             model_name='referenceemodel',
             name='child',
             field=models.ForeignKey(default=None,
 on_delete=django.db.models.deletion.CASCADE,
 to='index_error.SecondReferencedModel'),
         ),
     ]

 }}}

 Third migration explodes with following error:

 {{{django.db.utils.ProgrammingError: relation
 "index_error_referenceemodel_child_id_59ba6fc6" already exist }}}

 Which is due to the

 1) Field `` child = models.ForeignKey(ReferencedModel, ...)`` generates
 postgresql index named ``index_error_referenceemodel_child_id_59ba6fc6``
 2) RenameField operation doesn't update index name (but changes field
 name)
 3) When I try to create new field index names clash.

 My setup:

 * Django 1.11 (also broken in 2.0)
 * postgresql database

-- 
Ticket URL: <https://code.djangoproject.com/ticket/28803>
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 django-updates+unsubscr...@googlegroups.com.
To post to this group, send email to django-updates@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/049.14af4f8b0a5b5b8d371ca057b9364554%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to