[Django] #26782: DB Index key the same as Unique constraint key in seperate migrations

5 views
Skip to first unread message

Django

unread,
Jun 20, 2016, 1:45:52 PM6/20/16
to django-...@googlegroups.com
#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.

Django

unread,
Jun 20, 2016, 2:03:20 PM6/20/16
to django-...@googlegroups.com
#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 | Resolution:

Keywords: db, migration | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------
Changes (by mackeyja92):

* needs_better_patch: => 0
* needs_tests: => 0
* needs_docs: => 0


Old description:

New description:

Migration 1

{{{
class Migration(migrations.Migration):

}}}

Migration 2

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

--

--
Ticket URL: <https://code.djangoproject.com/ticket/26782#comment:1>

Django

unread,
Jun 20, 2016, 2:29:25 PM6/20/16
to django-...@googlegroups.com
#26782: DB Index key the same as Unique constraint key in seperate migrations
-------------------------------+--------------------------------------
Reporter: mackeyja92 | Owner: nobody
Type: Bug | Status: closed
Component: Uncategorized | Version: 1.8
Severity: Normal | Resolution: duplicate

Keywords: db, migration | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------
Changes (by charettes):

* status: new => closed
* resolution: => duplicate


Comment:

Duplicate of #25694.

--
Ticket URL: <https://code.djangoproject.com/ticket/26782#comment:2>

Reply all
Reply to author
Forward
0 new messages