[Django] #36550: AssertionError raised when bulk creation model with composite primary key

3 views
Skip to first unread message

Django

unread,
Aug 13, 2025, 8:26:12 AMAug 13
to django-...@googlegroups.com
#36550: AssertionError raised when bulk creation model with composite primary key
-------------------------------------+-------------------------------------
Reporter: Will-Ruddick | Type:
| Uncategorized
Status: new | Component: Database
| layer (models, ORM)
Version: 5.2 | Severity: Normal
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
I have a model that looks something like this

{{{
class CompositeModel(models.Model):
pk = models.CompositePrimaryKey("other_model", "the_time",
"created_at")

other_model = models.ForeignKey("test.OtherModel",
on_delete=models.CASCADE)
the_time = models.DateTimeField()
created_at = models.DateTimeField(auto_now_add=True)

}}}


And when I try and bulk create it in the code I get the following
exception.


{{{
CompositeModel.objects.bulk_create(new_items)
File "django/db/models/manager.py", line 87, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "django/db/models/query.py", line 836, in bulk_create
assert len(returned_columns) == len(objs_without_pk)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AssertionError
}}}


Where `returned_columns` is an empty list and `objs_without_pk ` is a list
of the objects I'm trying to create.

If I use `ignore_conflicts=True` then everything works fine and the
objects are created properly in the DB. I think this is a bug, but it
could be user error.

This is happening on version 5.2.5
--
Ticket URL: <https://code.djangoproject.com/ticket/36550>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Aug 13, 2025, 8:27:14 AMAug 13
to django-...@googlegroups.com
#36550: AssertionError raised when bulk creation model with composite primary key
-------------------------------------+-------------------------------------
Reporter: Will-Ruddick | Owner: (none)
Type: Uncategorized | Status: new
Component: Database layer | Version: 5.2
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Description changed by Will-Ruddick:

Old description:

> I have a model that looks something like this
>
> {{{
> class CompositeModel(models.Model):
> pk = models.CompositePrimaryKey("other_model", "the_time",
> "created_at")
>
> other_model = models.ForeignKey("test.OtherModel",
> on_delete=models.CASCADE)
> the_time = models.DateTimeField()
> created_at = models.DateTimeField(auto_now_add=True)
>
> }}}
>

> And when I try and bulk create it in the code I get the following
> exception.
>

> {{{
> CompositeModel.objects.bulk_create(new_items)
> File "django/db/models/manager.py", line 87, in manager_method
> return getattr(self.get_queryset(), name)(*args, **kwargs)
> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> File "django/db/models/query.py", line 836, in bulk_create
> assert len(returned_columns) == len(objs_without_pk)
> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> AssertionError
> }}}
>

> Where `returned_columns` is an empty list and `objs_without_pk ` is a
> list of the objects I'm trying to create.
>
> If I use `ignore_conflicts=True` then everything works fine and the
> objects are created properly in the DB. I think this is a bug, but it
> could be user error.
>
> This is happening on version 5.2.5

New description:

I have a model that looks something like this

{{{
class CompositeModel(models.Model):
pk = models.CompositePrimaryKey("other_model", "the_time",
"created_at")

other_model = models.ForeignKey("test.OtherModel",
on_delete=models.CASCADE)
the_time = models.DateTimeField()
created_at = models.DateTimeField(auto_now_add=True)

}}}


And when I try and bulk create it in the code I get the following
exception.


{{{
CompositeModel.objects.bulk_create(new_items)
File "django/db/models/manager.py", line 87, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "django/db/models/query.py", line 836, in bulk_create
assert len(returned_columns) == len(objs_without_pk)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AssertionError
}}}


Where `returned_columns` is an empty list and `objs_without_pk ` is a list
of the objects I'm trying to create.

If I use `ignore_conflicts=True` then everything works fine and the
objects are created properly in the DB. I think this is a bug, but it
could be user error.

This is happening on version 5.2.5 with a postgres DB

--
--
Ticket URL: <https://code.djangoproject.com/ticket/36550#comment:1>

Django

unread,
Aug 13, 2025, 9:49:40 AMAug 13
to django-...@googlegroups.com
#36550: AssertionError raised when bulk creation model with composite primary key
-------------------------------------+-------------------------------------
Reporter: Will-Ruddick | Owner: (none)
Type: Bug | Status: new
Component: Database layer | Version: 5.2
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Will-Ruddick):

* type: Uncategorized => Bug

--
Ticket URL: <https://code.djangoproject.com/ticket/36550#comment:2>
Reply all
Reply to author
Forward
0 new messages