#26782: DB Index key the same as Unique constraint key in seperate migrations
-------------------------------+---------------------------
     Reporter:  mackeyja92     |      Owner:  nobody
         Type:  Bug            |     Status:  new
    Component:  Uncategorized  |    Version:  1.8
     Severity:  Normal         |   Keywords:  db, migration
 Triage Stage:  Unreviewed     |  Has patch:  0
Easy pickings:  0              |      UI/UX:  0
-------------------------------+---------------------------
 When running a migration that has a db_index and a unique constraint it
 seems the same key is being used for both causing an error when applying
 migrations.

 Migration 1

 {{{
 class Migration(migrations.Migration):

     dependencies = [
         ('app', '0028_xxxx'),
     ]

     operations = [
         migrations.AlterField(
             model_name='model',
             name='uuid',
             field=models.CharField(db_index=True, max_length=36,
 blank=True, null=True, unique=False),
         ),
     ]

 }}}

 This migration runs the following SQL:

 {{{
 ALTER TABLE "table" ALTER COLUMN "uuid" DROP NOT NULL; (params [])
 CREATE INDEX "table_uuid_42f8156744ac727f_uniq" ON "table" ("uuid");
 (params [])
 CREATE INDEX "table_uuid_42f8156744ac727f_like" ON "table" ("uuid"
 varchar_pattern_ops); (params [])
 }}}

 Migration 2

 {{{
 def fill_uuids(apps, schema_editor):
     models = apps.get_model('app', 'model')
     models.objects.all().update(uuid=UUID1Now()) # Using postgres to
 generate uuid 1's


 def reverse_uuids(apps, schema_editor):
     models = apps.get_model('app', 'model')
     models.objects.all().update(uuid='')


 class Migration(migrations.Migration):
     dependencies = [
         ('app', '0029_xxxxx'),
     ]

     operations = [
         migrations.RunPython(fill_uuids, reverse_code=reverse_uuids),
         migrations.AlterField(
             model_name='model',
             name='uuid',
             field=models.CharField(db_index=True, max_length=36,
 blank=True, null=False, unique=True),
         ),
     ]
 }}}

 Migration 2 generates this SQL:

 {{{
 (0.146) UPDATE "table" SET "uuid" = uuid_generate_v1(); args=()
 ALTER TABLE "table" ALTER COLUMN "uuid" SET DEFAULT %s, ALTER COLUMN
 "uuid" SET NOT NULL; (params [u''])
 (0.002) ALTER TABLE "table" ALTER COLUMN "uuid" SET DEFAULT '', ALTER
 COLUMN "uuid" SET NOT NULL; args=[u'']
 ALTER TABLE "table" ADD CONSTRAINT "table_uuid_42f8156744ac727f_uniq"
 UNIQUE ("uuid"); (params [])
 (0.010) ALTER TABLE "table" ADD CONSTRAINT
 "table_uuid_42f8156744ac727f_uniq" UNIQUE ("uuid"); args=[]
 }}}

 That causes this exception `django.db.utils.ProgrammingError: relation
 "table_uuid_42f8156744ac727f_uniq" already exists`

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

Reply via email to