microsoft/mssql-django

Model.objects.bulk_create() does not return PK's in version 1.2

koloszko opened this issue · 4 comments

I have the following model:

class BBox(models.Model):
   pass

class BBoxProduct(models.Model):
    bbox = models.ForeignKey(BBox, on_delete=models.CASCADE)

In version 1.1.3 the following code run without problems:

models.BBox.objects.bulk_create(bboxes_to_insert)
models.BBoxProduct.objects.bulk_create(bboxes_products_to_insert)

where bboxes_products_to_insert is the list of BBoxProduct entities assigned to BBox entities from bboxes_to_insert list.

In version 1.2 it ends with error:
bulk_create() prohibited to prevent data loss due to unsaved related object 'bbox'

When I debug it I see that BBox entities doe not have PK's set after first bulk_create

Currently looking into this. What Django version and SQL Server version are you using?

I am using version 3.2.8

As far as I understand I think this could be the reason.

  • If you give the Release Notes for the version 1.2 a look MSSQL Django 1.2, you will find out bulk insert does not return any ids now or I should say it is disabled by default.
  • You can turn it on by setting return_rows_bulk_insert to True in OPTIONS in DATABASE settings.

Setting return_rows_bulk_insert to True solves the problem.