[Solved]-Django Migration Is Failing

10👍

It looks like you are running into this documented Django bug. The bug was triaged as invalid (and rightly so), so unfortunately there is no clean solution to the problem.

The unique together constraint in the database definition is shown as

"package_speciality_id_3aeb5c97679442e4_uniq" UNIQUE CONSTRAINT,
btree (speciality_id, title)

If you want to remove this constraint, you will need to ensure the unique_together definition in the migration file is consistent with the database definition. Try replacing the AlterUniqueTogether line with this:

migrations.AlterUniqueTogether(
    name='package',
    unique_together=set([('speciality_id', 'title')]),
),

8👍

This is an alternative if answer from arjun27 no solving your problem.

Today I got same problem (exact keyword with the question) when I tried to remove unique_together from Django Model. I’m using Django 1.11.

Read documentation and reference from arjun27, as I understand, the migration process will try to find UNIQUE CONTRAINST on Postgresql for field (speciality, title), using above case.

I try to find the UNIQUE CONSTRAINT on table but not found.

so, I create the UNIQUE CONSTRAINT directly from SQL console.

ALTER TABLE package ADD UNIQUE (speciality, title)

then I re-run the migration.

hope it helps.

2👍

Just in case anyone runs into the same issue as I did:
I have a multi-tenant system wherein each tenant has its own schema, with the public schema left empty.

When Django tries to inspect the current state of the database to delete the real unique constraint matching the historical one, it only looks inside the public schema, ignoring the schema information set in OPTIONS from the DATABASES setting.

There are a couple of tickets open regarding this issue, with no real resolution in sight: #6148 and #22673.

You can always write your own database backend to circumvent the issue, or submit a pull-request to Django!

Leave a comment