[Django]-Received "ValueError: Found wrong number (0) of constraints for …" during Django migration

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.

πŸ‘€rrauenza

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.

πŸ‘€Ben

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)

πŸ‘€Jolly

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.

πŸ‘€J_F4C

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

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.

πŸ‘€starboy_jb

Leave a comment