Received “ValueError: Found wrong number (0) of co

2019-06-14 23:00发布

问题:

While using Django 1.7 migrations, 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 an AlterUniqueTogether rule:

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

Reading up on bugs and such in the Django bug DB it seems to be about the existing unique_together in the db not matching the migration history.

How can I work around this error and finish my migrations?

回答1:

(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.