Opened 8 years ago

Closed 8 years ago

Last modified 8 years ago

#27385 closed Bug (fixed)

Error in QuerySet.bulk_create() on PostgreSQL when objs length is a multiple plus one of batch_size

Reported by: David Barragán Merino Owned by: nobody
Component: Database layer (models, ORM) Version: 1.10
Severity: Release blocker Keywords:
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Tim Graham)

With the model:

from django.db import models

class TestModel(models.Model):
    number = models.IntegerField()

If I try to do this on PostgreSQL:

objs = [TestModel(number=n) for n in range(11)]
TestModel.objects.bulk_create(objs, batch_size=10)

I get this error:

/home/bameda/.virtualenvs/taiga/lib/python3.5/site-packages/django/db/models/manager.py in manager_method(self, *args, **kwargs)
     83         def create_method(name, method):
     84             def manager_method(self, *args, **kwargs):
---> 85                 return getattr(self.get_queryset(), name)(*args, **kwargs)
     86             manager_method.__name__ = method.__name__
     87             manager_method.__doc__ = method.__doc__

/home/bameda/.virtualenvs/taiga/lib/python3.5/site-packages/django/db/models/query.py in bulk_create(self, objs, batch_size)
    450                 if objs_without_pk:
    451                     fields = [f for f in fields if not isinstance(f, AutoField)]
--> 452                     ids = self._batched_insert(objs_without_pk, fields, batch_size)
    453                     if connection.features.can_return_ids_from_bulk_insert:
    454                         assert len(ids) == len(objs_without_pk)

/home/bameda/.virtualenvs/taiga/lib/python3.5/site-packages/django/db/models/query.py in _batched_insert(self, objs, fields, batch_size)
   1062                 inserted_id = self._insert(item, fields=fields, using=self.db, return_id=True)
   1063                 if len(objs) > 1:
-> 1064                     inserted_ids.extend(inserted_id)
   1065                 if len(objs) == 1:
   1066                     inserted_ids.append(inserted_id)

TypeError: 'int' object is not iterable

The patch https://github.com/django/django/pull/7433 solved it.

It happens in master and in 1.10.x

Change History (3)

comment:1 by Tim Graham, 8 years ago

Description: modified (diff)
Severity: NormalRelease blocker
Summary: Error in bulk_create() when objs length is a multiple plus one of batch_sizeError in QuerySet.bulk_create() on PostgreSQL when objs length is a multiple plus one of batch_size
Triage Stage: UnreviewedReady for checkin

comment:2 by Tim Graham <timograham@…>, 8 years ago

Resolution: fixed
Status: newclosed

In b3bd3aa:

Fixed #27385 -- Fixed QuerySet.bulk_create() on PostgreSQL when the number of objects is a multiple plus one of batch_size.

comment:3 by Tim Graham <timograham@…>, 8 years ago

In 736f7e7e:

[1.10.x] Fixed #27385 -- Fixed QuerySet.bulk_create() on PostgreSQL when the number of objects is a multiple plus one of batch_size.

Backport of b3bd3aa07c026239dd39d1a37498dfd0a2f09caf from master

Note: See TracTickets for help on using tickets.
Back to Top