Received "ValueError: Found wrong number (0) of constraints for ..." during Django migration
(Postgres and MySQL Answer)
If you look at your actual table (use \d table_name
) and look at the indexes, you'll find an entry for your unique constraint. This is what Django is trying to find and drop. But it can't find an exact match.
For example,
"table_name_...6cf2a9c6e98cbd0d_uniq" UNIQUE CONSTRAINT, btree (d, a, b, c)
In my case, the order of the keys (d, a, b, c)
did not match the constraint it was looking to drop (a, b, c, d)
.
I went back into my migration history and changed the original AlterUniqueTogether
to match the actual order in the database.
The migration then completed successfully.
I had a similar issue come up while I was switching over a CharField to become a ForeignKey. Everything worked with that process, but I was left with Django thinking it still needed to update the unique_together
in a new migration. (Even though everything looked correct from inside postgres.) Unfortunately applying this new migration would then give a similar error:
ValueError: Found wrong number (0) of constraints for program(name, funder, payee, payer, location, category)
The fix that ultimately worked for me was to comment out all the previous AlterUniqueTogether
operations for that model. The manage.py migrate
worked without error after that.