# INSERT IGNORE in Django ORM

**URL:** <https://forum.djangoproject.com/t/insert-ignore-in-django-orm/24708>\
**Category:** Using the ORM\
**Created:** [October 22, 2023, 8:53am UTC](https://forum.djangoproject.com/t/insert-ignore-in-django-orm/24708 "2023-10-22T08:53:10Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![anjanesh](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/anjanesh/32/4583_2.png) [@anjanesh](https://forum.djangoproject.com/u/anjanesh)\
**Post date:** [October 22, 2023, 8:53am UTC](https://forum.djangoproject.com/t/insert-ignore-in-django-orm/24708/1 "2023-10-22T08:53:11Z")

</div>

How do I do an INSERT IGNORE in Django ORM ?

```auto
 portal.save() # IGNORE if on DUPLICATE_KEY

```

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [October 22, 2023, 2:13pm UTC](https://forum.djangoproject.com/t/insert-ignore-in-django-orm/24708/2 "2023-10-22T14:13:14Z")

</div>

It’s not clear what you’re looking to do here.

Assuming `portal` is an instance of an object, save is either going to update an existing row (one with an existing pk) or insert it (if the pk field is null).

---

<div class="post-metadata">

**Author:** ![adamchainz](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/adamchainz/32/26_2.png) [@adamchainz](https://forum.djangoproject.com/u/adamchainz)\
**Post date:** [October 22, 2023, 9:55pm UTC](https://forum.djangoproject.com/t/insert-ignore-in-django-orm/24708/3 "2023-10-22T21:55:27Z")

</div>

[`update_or_create`](https://docs.djangoproject.com/en/stable/ref/models/querysets/#django.db.models.query.QuerySet.update_or_create) with empty `defaults` may be what you’re looking for.

---

<div class="post-metadata">

**Author:** ![anjanesh](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/anjanesh/32/4583_2.png) [@anjanesh](https://forum.djangoproject.com/u/anjanesh)\
**Post date:** [October 23, 2023, 1:27am UTC](https://forum.djangoproject.com/t/insert-ignore-in-django-orm/24708/4 "2023-10-23T01:27:00Z")

</div>

What I got from ChatGPT :

```auto
        # Create a new Portal object and populate its fields using the data from the dictionary
        portal, created = Portal.objects.get_or_create(
            name_id=portal_data['name_id'],
            defaults={
                'name': portal_data['name'],
                'service_provider': portal_data['provider_id'],
                # Populate other fields as needed
            }
        )

        # If the portal was created, save it to the database
        if created:
            portal.save()

        cred_id = saveCredential(portal)

```

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [October 23, 2023, 1:41am UTC](https://forum.djangoproject.com/t/insert-ignore-in-django-orm/24708/5 "2023-10-23T01:41:08Z")

</div>

Well gee. To be kind, that’s redundant and unnecessary.

The [`get_or_create`](https://docs.djangoproject.com/en/4.2/ref/models/querysets/#get-or-create) method creates the object. There is no value to having the `if / save` block in the code.

If you explain what the underlying objective is, we may be able to suggest an actual solution that satisfies your requirements.

---

<div class="post-metadata">

**Author:** ![anjanesh](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/anjanesh/32/4583_2.png) [@anjanesh](https://forum.djangoproject.com/u/anjanesh)\
**Post date:** [October 23, 2023, 1:44am UTC](https://forum.djangoproject.com/t/insert-ignore-in-django-orm/24708/6 "2023-10-23T01:44:21Z")

</div>

I need to save the portal into the database and get the newly generated portal\_id - and if it already exists I need to get the existing portal id.  
I need the portal id to be used in inserting it into another related table.

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [October 23, 2023, 1:44am UTC](https://forum.djangoproject.com/t/insert-ignore-in-django-orm/24708/7 "2023-10-23T01:44:55Z")

</div>

The `get_or_create` method does that for you. No additional code is necessary.

---

<div class="post-metadata">

**Author:** ![anjanesh](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/anjanesh/32/4583_2.png) [@anjanesh](https://forum.djangoproject.com/u/anjanesh)\
**Post date:** [October 25, 2023, 4:09am UTC](https://forum.djangoproject.com/t/insert-ignore-in-django-orm/24708/8 "2023-10-25T04:09:05Z")

</div>

> [@anjanesh](#):
>
> `Portal.objects.get_or_create`

I guess ChatGPT thinks that `Portal.objects.get_or_create` creates the object but doesn’t yet save to the database ?

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [October 25, 2023, 4:20am UTC](https://forum.djangoproject.com/t/insert-ignore-in-django-orm/24708/9 "2023-10-25T04:20:28Z")

</div>

ChatGPT doesn’t “think”. It emits text based on other text that it has processed to find patterns. Unfortunately, we can have no real idea as to what text it has processed to identify those patterns.

It is the untrusted sources of that generated text that cause me to strongly recommend that anyone using it _always_ independently verify the code generated by it, and not just blindly accept it as a solution. You just never know what the side effects or vulnerabilities may be.

I agree - it can be a useful tool. But like other resources I can name (`<cough>SO</cough>`), it’s a starting point and not an end point.
