Opened 8 years ago

Last modified 8 years ago

#26782 closed Bug

DB Index key the same as Unique constraint key in seperate migrations — at Version 1

Reported by: Jared Mackey Owned by: nobody
Component: Uncategorized Version: 1.8
Severity: Normal Keywords: db, migration
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Jared Mackey)

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

A work around is to remove the db_index in migration 1.

Change History (1)

comment:1 by Jared Mackey, 8 years ago

Description: modified (diff)
Note: See TracTickets for help on using tickets.
Back to Top