0

I am using unique_together constraint in my model.

class Facility(models.Model):
    institute = models.ForeignKey(Institute)
    name = models.CharField(max_length=100)

    class Meta:
        unique_together = ('institute', 'name',)

How can I get the duplicate values with same name and institute?

Rakesh babu
  • 325
  • 6
  • 11
  • You can't, the constraint makes sure, that anytime you will try to insert (update) a duplicate value with the `'institute'` and `'name'`, that already exist in the table, an exception will be raised. – Nikita Apr 13 '16 at 07:32

1 Answers1

0

After set meta unique_together

it's mean when you try to save duplicate value in DB Django not allow this and throw an exception ,means there is no duplicate value in DB.

According to your model structure if you don't want to save duplicate entries in DB then delete all previous entries first then run make migrations and migrate.

GrvTyagi
  • 4,231
  • 1
  • 33
  • 40
  • Actually I have some data in database. Later I want to add that unique_together so before that i want to check once – Rakesh babu Apr 13 '16 at 09:42
  • SELECT y.id,y.name,y.email FROM YourTable y INNER JOIN (SELECT name,email, COUNT(*) AS CountOf FROM YourTable GROUP BY name,email HAVING COUNT(*)>1 ) dt ON y.name=dt.name and y.email=dt.email – GrvTyagi Apr 13 '16 at 09:49
  • Use this query for get duplicate data from table. Note: Change all veriable names accordingly like: GROUP BY institute,name – GrvTyagi Apr 13 '16 at 09:50
  • follow this for more operations : http://stackoverflow.com/questions/2594829/finding-duplicate-values-in-a-sql-table – GrvTyagi Apr 13 '16 at 09:53