Received "ValueError: An invalid number (0) of restrictions was found for ..." during a Django migration

When using Django 1.7 migration, I came across a migration that worked in development but not in production:

ValueError: Found wrong number (0) of constraints for table_name(a, b, c, d)

This is caused by the AlterUniqueTogether rule:

  migrations.AlterUniqueTogether( name='table_name', unique_together=set([('a', 'b')]), ) 

Reading errors, etc. in DB DB errors seem to be related to the existing unique_together in db, not matching the migration history.

How can I get around this error and complete my migrations?

+21
source share
2 answers

(Postgres and MySQL answer)

If you look at your actual table (use \d table_name ) and look at the indexes, you will find an entry for your unique constraint. This is what Django is trying to find and drop. But he cannot find an exact match.

For instance,

 "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 restriction that he would like to remove (a, b, c, d) .

I went back to the migration history and modified the original AlterUniqueTogether to match the actual order in the database.

The migration is then completed successfully.

+30
source

I had a similar problem when I switched CharField to become a ForeignKey. Everything worked with this process, but I stayed with Django thinking that I still need to update unique_together in the new migration. (Even if everything looked right from inside postgres.) Unfortunately, applying this new migration will result in a similar error:

ValueError: Found wrong number (0) of constraints for program(name, funder, payee, payer, location, category)

The fix that eventually worked for me was to comment out all previous AlterUniqueTogether operations for this model. manage.py migrate worked without errors after that.

0
source

Source: https://habr.com/ru/post/1262712/


All Articles