#28542: migration that introduces uuid field is not reversible with unique=True
--------------------------------------+------------------------
               Reporter:  karyon      |          Owner:  nobody
                   Type:  Bug         |         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           |
--------------------------------------+------------------------
 sorry for the confusing title, couldn't think of a better one...

 this migration here (from https://github.com/fsr-
 itse/EvaP/pull/1002/files)

 {{{
      # -*- coding: utf-8 -*-
 # Generated by Django 1.11.3 on 2017-07-03 18:31
 from __future__ import unicode_literals

 from django.db import migrations, models
 import uuid

 def fill_textanswer_uuid(apps, schema_editor):
     db_alias = schema_editor.connection.alias
     TextAnswer = apps.get_model('evaluation', 'TextAnswer')
     for obj in TextAnswer.objects.using(db_alias).all():
         obj.uuid = uuid.uuid4()
         obj.save()

 class Migration(migrations.Migration):
     """ this migration changes a model from a auto-generated id field to a
 uuid-primary key. """

     dependencies = [
         ('evaluation', '0059_add_yes_no_questions'),
     ]

     # Based on
     # https://gist.github.com/smcoll/8bb867dc631433c01fd0

     operations = [
         migrations.AddField(
             model_name='textanswer',
             name='uuid',
             field=models.UUIDField(null=True),
         ),
         migrations.RunPython(fill_textanswer_uuid,
 migrations.RunPython.noop),
         migrations.AlterField(
             model_name='textanswer',
             name='uuid',
             field=models.UUIDField(primary_key=False, default=uuid.uuid4,
 serialize=False, editable=False), # add 'unique=True' here
         ),
         migrations.RemoveField('TextAnswer', 'id'),
         migrations.RenameField(
             model_name='textanswer',
             old_name='uuid',
             new_name='id'
         ),
         migrations.AlterField(
             model_name='textanswer',
             name='id',
             field=models.UUIDField(primary_key=True, default=uuid.uuid4,
 serialize=False, editable=False),
         ),
     ]

 }}}

 runs fine forwards and backwards.

 adding unique=True in the line indicated above makes the backward
 direction fail, although it should intuitively have little to no effect.
 especially the error {{{multiple primary keys for table
 "evaluation_textanswer" are not allowed}}} doesn't make much sense to me.
 traceback:

 {{{
 Traceback (most recent call last):
   File "/home/vagrant/.local/lib/python3.4/site-
 packages/django/db/backends/utils.py", line 65, in execute
     return self.cursor.execute(sql, params)
 psycopg2.ProgrammingError: multiple primary keys for table
 "evaluation_textanswer" are not allowed


 The above exception was the direct cause of the following exception:

 Traceback (most recent call last):
   File "./manage.py", line 10, in <module>
     execute_from_command_line(sys.argv)
   File "/home/vagrant/.local/lib/python3.4/site-
 packages/django/core/management/__init__.py", line 363, in
 execute_from_command_line
     utility.execute()
   File "/home/vagrant/.local/lib/python3.4/site-
 packages/django/core/management/__init__.py", line 355, in execute
     self.fetch_command(subcommand).run_from_argv(self.argv)
   File "/home/vagrant/.local/lib/python3.4/site-
 packages/django/core/management/base.py", line 283, in run_from_argv
     self.execute(*args, **cmd_options)
   File "/home/vagrant/.local/lib/python3.4/site-
 packages/django/core/management/base.py", line 330, in execute
     output = self.handle(*args, **options)
   File "/home/vagrant/.local/lib/python3.4/site-
 packages/django/core/management/commands/migrate.py", line 204, in handle
     fake_initial=fake_initial,
   File "/home/vagrant/.local/lib/python3.4/site-
 packages/django/db/migrations/executor.py", line 119, in migrate
     state = self._migrate_all_backwards(plan, full_plan, fake=fake)
   File "/home/vagrant/.local/lib/python3.4/site-
 packages/django/db/migrations/executor.py", line 194, in
 _migrate_all_backwards
     self.unapply_migration(states[migration], migration, fake=fake)
   File "/home/vagrant/.local/lib/python3.4/site-
 packages/django/db/migrations/executor.py", line 264, in unapply_migration
     state = migration.unapply(state, schema_editor)
   File "/home/vagrant/.local/lib/python3.4/site-
 packages/django/db/migrations/migration.py", line 178, in unapply
     operation.database_backwards(self.app_label, schema_editor,
 from_state, to_state)
   File "/home/vagrant/.local/lib/python3.4/site-
 packages/django/db/migrations/operations/fields.py", line 160, in
 database_backwards
     schema_editor.add_field(from_model,
 to_model._meta.get_field(self.name))
   File "/home/vagrant/.local/lib/python3.4/site-
 packages/django/db/backends/base/schema.py", line 429, in add_field
     self.execute(sql, params)
   File "/home/vagrant/.local/lib/python3.4/site-
 packages/django/db/backends/base/schema.py", line 120, in execute
     cursor.execute(sql, params)
   File "/home/vagrant/.local/lib/python3.4/site-
 packages/django/db/backends/utils.py", line 80, in execute
     return super(CursorDebugWrapper, self).execute(sql, params)
   File "/home/vagrant/.local/lib/python3.4/site-
 packages/django/db/backends/utils.py", line 65, in execute
     return self.cursor.execute(sql, params)
   File "/home/vagrant/.local/lib/python3.4/site-
 packages/django/db/utils.py", line 94, in __exit__
     six.reraise(dj_exc_type, dj_exc_value, traceback)
   File "/home/vagrant/.local/lib/python3.4/site-
 packages/django/utils/six.py", line 685, in reraise
     raise value.with_traceback(tb)
   File "/home/vagrant/.local/lib/python3.4/site-
 packages/django/db/backends/utils.py", line 65, in execute
     return self.cursor.execute(sql, params)
 django.db.utils.ProgrammingError: multiple primary keys for table
 "evaluation_textanswer" are not allowed
 }}}

 console output including sql statements when running the migration
 backwards without the unique=True: https://pastebin.com/gs2SaBjt
 with unique=True: https://pastebin.com/QUebX0aa

 check them out, the sql is vastly different. i haven't investigated
 further what the problem is.

-- 
Ticket URL: <https://code.djangoproject.com/ticket/28542>
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/049.f666506b38bd8f9e1086d1309797fc20%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to