Django Unique Together (with foreign keys)

You can’t. The unique_together clause is directly translated to the SQL unique index. And you can only set those on columns of a single table, not a combination of several tables. You can add validation for it yourself though, simply overwrite the validate_unique method and add this validation to it. Docs: http://docs.djangoproject.com/en/dev/ref/models/instances/#django.db.models.Model.validate_unique

A Queue that ensure uniqueness of the elements?

How about a LinkedHashSet? Its iterator preserves insertion order, but because it’s a Set, its elements are unique. As its documentation says, Note that insertion order is not affected if an element is re-inserted into the set. In order to efficiently remove elements from the head of this “queue”, go through its iterator: Iterator<?> i … Read more

How to delete duplicate entries?

Some of these approaches seem a little complicated, and I generally do this as: Given table table, want to unique it on (field1, field2) keeping the row with the max field3: DELETE FROM table USING table alias WHERE table.field1 = alias.field1 AND table.field2 = alias.field2 AND table.max_field < alias.max_field For example, I have a table, … Read more

Unique Constraint in Entity Framework Code First

As far as I can tell, there’s no way to do this with Entity Framework at the moment. However, this isn’t just a problem with unique constraints… you may want to create indexes, check constraints, and possibly triggers and other constructs too. Here’s a simple pattern you can use with your code-first setup, though admittedly … Read more