37

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?

8 Answers 8

56

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

4
  • Thanks a ton for sharing this. I'm using MySQL and had the same issue with the order of the fields in the DB constraint not matching the order of the fields in the original migration file that added the unique_together.
    – dshap
    Feb 16, 2018 at 6:56
  • I thought I'll be tricky and will go with two migrations: remove the existing constraint and then add the new extended one. But even the removal is unsuccessful due to this error, so I need to reorder. :P
    – Csaba Toth
    Mar 30, 2018 at 18:29
  • Oh, and this is true for MySQL as well, I'm working with that
    – Csaba Toth
    Mar 30, 2018 at 19:29
  • This is so useful, thanks. Actually helped me learn more about how migrations work- ie, the looking back at previous migrations when reversing.
    – rbennell
    Jun 23, 2021 at 12:27
8

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.

0
2

"unique_together in the db not matching the migration history" - Every time an index is altered on a table it checks its previous index and drops it. In your case it is not able to fetch the previous index.

Solution- 1.Either you can generate it manually 2.Or revert to code where previous index is used and migrate.Then finally change to new index in your code and run migration.(django_migration files to be taken care of)

0

Also worth checking that you only have the expected number of Unique indexes for the table in question.

For example, if your table has multiple Unique indexes, then you should delete them to make sure you have only 1 (or whatever the number of expected Unique indexes is) pre-migration index present.

To check how many Unique indexes are there for a given table in PostgreSQL:

SELECT *
FROM information_schema.table_constraints AS c
WHERE
    c.table_name = '<table_name>'
    and c.constraint_type = 'UNIQUE'
0

Just in case someone runs into this and the previous answers haven't solved, In my case the issue was that when I modified the unique together constraint, the migration was attempted but the data didn't allow it (because of a more restrictive unique together constraint). However, the migration managed to delete the unique together constraint from the table leaving it in an inconsistent state. I had to migrate back to zero and re-apply the migration without data. Then it went through without problems.

In summary, make sure your data will be able to accept the new constraint before you apply the migration.

0
  1. Find the latest migration file of the respective table, find unique together, and replace current unique constraints fields.
  2. Migrate database using ./manage.py migrate your_app_name.
  3. Revert or undo the previous migrations file.
0

In my case problem was that the previous migration was not present in the table dajsngo_migrations. I added missing entry and then the new migration worked

1
  • 1
    Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    Dec 20, 2021 at 10:38
0

Someone may get this issue while modifying the unique_together. Basically, the table state is not consistent with the migrations. You may need to add the previous constraints manually using MySQL shell.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.