# bulk\_create() method for related models

**URL:** https://forum.djangoproject.com/t/bulk-create-method-for-related-models/10112
**Category:** Using the ORM
**Created:** [October 14, 2021, 5:14pm UTC](https://forum.djangoproject.com/t/bulk-create-method-for-related-models/10112 "2021-10-14T17:14:25Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![mc613](https://avatars.discourse-cdn.com/v4/letter/m/ba8739/32.png) [@mc613](https://forum.djangoproject.com/u/mc613)
#### Post date: [October 14, 2021, 5:14pm UTC](https://forum.djangoproject.com/t/bulk-create-method-for-related-models/10112/1 "2021-10-14T17:14:25Z")

</div>

I have two models, A and B that are one-to-one related. How do I use Django’s [bulk\_create()](https://docs.djangoproject.com/en/3.2/ref/models/querysets/#bulk-create) to successfully create them together as a bulk? I’m using Django’s auto-generated IDs for both models. Thank you so much.

---

<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 14, 2021, 7:43pm UTC](https://forum.djangoproject.com/t/bulk-create-method-for-related-models/10112/2 "2021-10-14T19:43:45Z")

</div>

> [@mc613](#):
>
> How do I use Django’s [bulk\_create()](https://docs.djangoproject.com/en/3.2/ref/models/querysets/#bulk-create) to successfully create them together as a bulk?

You don’t. At least not as one call.

The bulk-create method is a method on a model class. You pass it something that can be converted to a `list` containing the set of the model being inserted.

What I believe you ought to be able to do is insert your base table entries first, then assign the appropriate key to the data being added to the tables relating to the base table, and do the bulk\_create on it. (Note: The set of objects being inserted through bulk\_create _can_ have the pk assigned when being inserted, even if it an “auto-generated id”.)

---

<div class="post-metadata">

### Author: ![mc613](https://avatars.discourse-cdn.com/v4/letter/m/ba8739/32.png) [@mc613](https://forum.djangoproject.com/u/mc613)
#### Post date: [October 14, 2021, 7:48pm UTC](https://forum.djangoproject.com/t/bulk-create-method-for-related-models/10112/3 "2021-10-14T19:48:54Z")

</div>

I tried executing two `bulk_create()`s and I tried to link them together with a `bulk_update()` but I get the error `ValueError: All bulk_update() objects must have a primary key set.`. How do I solve this please? Thank you.

---

<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 14, 2021, 7:49pm UTC](https://forum.djangoproject.com/t/bulk-create-method-for-related-models/10112/4 "2021-10-14T19:49:57Z")

</div>

What database engine are you using?

---

<div class="post-metadata">

### Author: ![mc613](https://avatars.discourse-cdn.com/v4/letter/m/ba8739/32.png) [@mc613](https://forum.djangoproject.com/u/mc613)
#### Post date: [October 14, 2021, 7:50pm UTC](https://forum.djangoproject.com/t/bulk-create-method-for-related-models/10112/5 "2021-10-14T19:50:51Z")

</div>

Postgres / Postgis (post must be at least 20 characters… so here it is …)

---

<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 14, 2021, 7:52pm UTC](https://forum.djangoproject.com/t/bulk-create-method-for-related-models/10112/6 "2021-10-14T19:52:38Z")

</div>

Do your first bulk\_create.

Then, assign the related field of the objects being used to populate the second table to their corresponding entries of the first table.

Then do your second bulk create.

---

<div class="post-metadata">

### Author: ![mc613](https://avatars.discourse-cdn.com/v4/letter/m/ba8739/32.png) [@mc613](https://forum.djangoproject.com/u/mc613)
#### Post date: [October 14, 2021, 8:56pm UTC](https://forum.djangoproject.com/t/bulk-create-method-for-related-models/10112/7 "2021-10-14T20:56:17Z")

</div>

Thank you Ken. Now I’m getting this error: `ValueError: bulk_create() prohibited to prevent data loss due to unsaved related object 'person'.`

```auto
people_records = [Person(name=‘A’, age=1), Person(name=‘B’, age=2)]

# people_records
# [<Person: A>, <Person: B>]

people = Person.objects.bulk_create(people_records)

# people
# <QuerySet [<Person: A>, <Person: B>]>

hobby_records = [Hobby(name=‘H1’, person = people[0]), Hobby(name=‘H2’, person=people[1]) ]

# hobby_records
# [<Hobby: H1>, <Hobby: H2>]

hobbies = Hobby.objects.bulk_create(hobby_records) 
# ValueError: bulk_create() prohibited to prevent data loss due to unsaved related object 'person'.

```

---

<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 14, 2021, 10:12pm UTC](https://forum.djangoproject.com/t/bulk-create-method-for-related-models/10112/8 "2021-10-14T22:12:17Z")

</div>

I can’t recreate these symptoms based on the code snippet here. I’ve done this in the Django shell and it works exactly as expected.

```auto
In [17]: def mp():
    ...: pr=[MPerson(name="C", age=3), MPerson(name="D", age=3)]
    ...: pe=MPerson.objects.bulk_create(pr)
    ...: hr=[MHobby(name='H1', person=pe[0]), MHobby(name='H2', person=pe[1])]
    ...: ho=MHobby.objects.bulk_create(hr)
    ...: 

```

(Pardon the class names - I mangle them to keep examples separate in my test environment.)

It’s possible that there’s some different in the model definition that would explain the different results. Can you post the models you’re using for this?

---

<div class="post-metadata">

### Author: ![Randix6644](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/randix6644/32/9614_2.png) [@Randix6644](https://forum.djangoproject.com/u/Randix6644)
#### Post date: [August 8, 2022, 6:57am UTC](https://forum.djangoproject.com/t/bulk-create-method-for-related-models/10112/9 "2022-08-08T06:57:01Z")

</div>

i have encountered the exact same problems.

---

<div class="post-metadata">

### Author: ![dev.named.jay](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/dev.named.jay/32/8823_2.png) [@dev.named.jay](https://forum.djangoproject.com/u/dev.named.jay)
#### Post date: [August 11, 2022, 4:03pm UTC](https://forum.djangoproject.com/t/bulk-create-method-for-related-models/10112/10 "2022-08-11T16:03:22Z")

</div>

Are you certain you don’t have a many-to-many relationship on your Person and Hobby models?
