#25752: RenameField on m2m causes error when new m2m field is added with old name -------------------------------------+------------------------------------- Reporter: skyjur | Owner: nobody Type: Bug | Status: new Component: Migrations | Version: 1.8 Severity: Normal | Resolution: Keywords: postgres migrations | Triage Stage: renamefield | Unreviewed Has patch: 0 | Needs documentation: 0 Needs tests: 0 | Patch needs improvement: 0 Easy pickings: 0 | UI/UX: 0 -------------------------------------+------------------------------------- Description changed by skyjur:
Old description: > When in migrations I do RenameField on ManyToMany field, and then do > AddField with ManyToMany with same name but different model, I get > Integrity Error referring to existing index. > > It seems the problem is that RenameField does not rename database > indexes, and then when new m2m field is being added it tries to create > index with same name and so I get database error: > > {{{ > ... > File "django/db/backends/utils.py", line 64, in execute > return self.cursor.execute(sql, params) > File "django/db/utils.py", line 97, in __exit__ > six.reraise(dj_exc_type, dj_exc_value, traceback) > File "django/db/backends/utils.py", line 64, in execute > return self.cursor.execute(sql, params) > django.db.utils.ProgrammingError: relation "cms_test_many_2e06cda4" > already exists > }}} > > Here is how migration which creates this error looks like: > > {{{ > class Migration(migrations.Migration): > > dependencies = [ > ] > > operations = [ > migrations.RenameField( > 'test', > 'many', > 'many_legacy', > ), > migrations.AddField( > model_name='test', > name='many', > field=models.ManyToManyField(to='cms.NewRel2'), > ), > ] > > }}} > > Initially I noticed this error on 1.7, then I also managed to replicate > it on 1.8 > > Error happened on postgres database. New description: Given I have a field {{{ many = ManyToManyField() }}} I create a migration: {{{ RenameField: "many" to "many_legacy" Create Field: "many" as ManyToManyField() }}} When I run the migration I get: {{{ ... File "django/db/backends/utils.py", line 64, in execute return self.cursor.execute(sql, params) File "django/db/utils.py", line 97, in __exit__ six.reraise(dj_exc_type, dj_exc_value, traceback) File "django/db/backends/utils.py", line 64, in execute return self.cursor.execute(sql, params) django.db.utils.ProgrammingError: relation "cms_test_many_2e06cda4" already exists }}} It seems the problem is that RenameField does not rename database indexes, and then when new m2m field is being added it tries to create index with same name and so I get database error. Here is full migration code which throws this error: {{{ class Migration(migrations.Migration): dependencies = [ ] operations = [ migrations.RenameField( 'test', 'many', 'many_legacy', ), migrations.AddField( model_name='test', name='many', field=models.ManyToManyField(to='cms.NewRel2'), ), ] }}} Initially I noticed this error on 1.7, then I also managed to replicate it on 1.8 Error happened on postgres database. -- -- Ticket URL: <https://code.djangoproject.com/ticket/25752#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 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/064.4cc8c6891fa9d56f5280fe1ef916ac1e%40djangoproject.com. For more options, visit https://groups.google.com/d/optout.