# get\_or\_create UNIQUE constraint error

**URL:** <https://forum.djangoproject.com/t/get-or-create-unique-constraint-error/36214>\
**Category:** Using the ORM\
**Created:** [November 5, 2024, 6:30pm UTC](https://forum.djangoproject.com/t/get-or-create-unique-constraint-error/36214 "2024-11-05T18:30:33Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![federicovisioli](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/federicovisioli/32/23622_2.png) [@federicovisioli](https://forum.djangoproject.com/u/federicovisioli)\
**Post date:** [November 5, 2024, 6:30pm UTC](https://forum.djangoproject.com/t/get-or-create-unique-constraint-error/36214/1 "2024-11-05T18:30:33Z")

</div>

Given the following Model:

```auto
class Model1(models.Model):
    m2mrelation = models.ManyToManyField('Model2', related_name='model2')
    field1 = models.CharField(max_length=14)
    data1 = models.FloatField()
    data2 = models.FloatField()
    data3 = models.FloatField()
    data4 = models.FloatField()
    data5 = models.FloatField()
    field2 = models.CharField(max_length=10)
    field3 = models.CharField(max_length=20)

    class Meta:
        indexes = [
            models.Index(fields=['field1', 'field2', 'field3']),
        ]

        unique_together = ['field1', 'field2', 'field3']

```

I’m trying to use the “get\_or\_create()” method to get or create a new Model1 inside a Model2 method:

```auto
for data in objects:
     object, created = self.model1.get_or_create(
          field1=field1,
          field2=field2,
          field3=field3,
          defaults={
                    'data1': float(data[1]),
                    'data2': float(data[2]),
                    'data3': float(data[3]),
                    'data4': float(data[4]),
                    'data5': float(data[5])
          }
     )

     if not object.m2mrelation.filter(id=self.id).exists():
          object.m2mrelation.add(self)

```

When I’m trying to create new objects, I’m getting the `IntegrityError: UNIQUE constraint failed` in the `get_or_create` method.

Am I missing something?

---

<div class="post-metadata">

**Author:** ![philgyford](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/philgyford/32/14295_2.png) [@philgyford](https://forum.djangoproject.com/u/philgyford)\
**Post date:** [November 5, 2024, 6:41pm UTC](https://forum.djangoproject.com/t/get-or-create-unique-constraint-error/36214/2 "2024-11-05T18:41:15Z")

</div>

I’d assume that one of the times you call that method, the values of at least two of `field1`, `field2`, and `field3` are the same.

---

<div class="post-metadata">

**Author:** ![federicovisioli](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/federicovisioli/32/23622_2.png) [@federicovisioli](https://forum.djangoproject.com/u/federicovisioli)\
**Post date:** [November 5, 2024, 6:44pm UTC](https://forum.djangoproject.com/t/get-or-create-unique-constraint-error/36214/3 "2024-11-05T18:44:40Z")

</div>

Hi @philgyford thank you for the answer.

Doesn’t `unique_together` only check if all three constraints are violated together? So, if only two of them are the same, shouldn’t it pass the check?

---

<div class="post-metadata">

**Author:** ![philgyford](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/philgyford/32/14295_2.png) [@philgyford](https://forum.djangoproject.com/u/philgyford)\
**Post date:** [November 5, 2024, 6:49pm UTC](https://forum.djangoproject.com/t/get-or-create-unique-constraint-error/36214/4 "2024-11-05T18:49:00Z")

</div>

Oh yes, sorry, I think you’re right. In which case I’d assume that all three are the same.

---

<div class="post-metadata">

**Author:** ![federicovisioli](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/federicovisioli/32/23622_2.png) [@federicovisioli](https://forum.djangoproject.com/u/federicovisioli)\
**Post date:** [November 5, 2024, 6:50pm UTC](https://forum.djangoproject.com/t/get-or-create-unique-constraint-error/36214/5 "2024-11-05T18:50:55Z")

</div>

But if all of them already exist together, doesn’t `get_or_create` return the existing instance instead of trying to create a new one?

---

<div class="post-metadata">

**Author:** ![philgyford](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/philgyford/32/14295_2.png) [@philgyford](https://forum.djangoproject.com/u/philgyford)\
**Post date:** [November 5, 2024, 7:20pm UTC](https://forum.djangoproject.com/t/get-or-create-unique-constraint-error/36214/6 "2024-11-05T19:20:59Z")

</div>

Oh, again, yes, you’re right. I’ve been trying to do too many things at once, so I’ll butt out of this 🙂

One thing that might help someone else help you is: can you clarify what you mean by “I’m trying to use the “get\_or\_create()” method to get or create a new Model1 inside a Model2 method” ?

The code you showed isn’t in a method. Can you give a slightly fuller example of what you’re actually doing?

---

<div class="post-metadata">

**Author:** ![federicovisioli](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/federicovisioli/32/23622_2.png) [@federicovisioli](https://forum.djangoproject.com/u/federicovisioli)\
**Post date:** [November 5, 2024, 9:25pm UTC](https://forum.djangoproject.com/t/get-or-create-unique-constraint-error/36214/7 "2024-11-05T21:25:15Z")

</div>

Hi @philgyford , I found the problem!

The issue was here: `object, created = self.model1.get_or_create`. I was using `self.model1` instead of `model1.objects.get_or_create`, so Django wasn’t searching for the record within the entire `model1` table.

Now it’s working! Thanks for your time!
