Django unique_together not preventing duplicates

Try the proper nested-tuple syntax ((foo,bar),) instead of just (foo, bar)?

https://docs.djangoproject.com/en/dev/ref/models/options/#unique-together


As aganders3 mentions the constraint is enforced at the database level; I assume though that you are using a database like SQLite that doesn't support this kind of constraint.

The reason that it all works as expected through the admin is that it is doing the uniqueness check itself (it doesn't rely strictly on the database to signal constraint violations).

You can switch to a database engine that supports this kind of uniqueness constraint (either MySQL or Postgres would work) or you could look at adding the check in using signals: http://djangosnippets.org/snippets/1628/


Yes the paremeter unique_together receives as input a tuple of tuples, I have not tested tuples of more than two elements but it should work

for your example:

unique_together = (("teamID", "name"), ("slug", "teamNumber"))

or:

unique_together = (("teamID", "name", "slug", "teamNumber", "totalScore"))

Tags:

Python

Django