#36060: IntegrityError: null value in column "_order" when bulk_create()
-------------------------------------+-------------------------------------
     Reporter:  Nikolay Fedorov      |                    Owner:  (none)
         Type:  Bug                  |                   Status:  new
    Component:  Database layer       |                  Version:  5.1
  (models, ORM)                      |
     Severity:  Normal               |               Resolution:
     Keywords:  bulk_create,         |             Triage Stage:  Accepted
  order_with_respect_to              |
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------
Changes (by Simon Charette):

 * stage:  Unreviewed => Accepted

Comment:

 This is effectively not something `bulk_create` currently implements.

 It's missing
 
[https://github.com/django/django/blob/51df0dff3c4f28016185a9e876ee5b3420712f99/django/db/models/base.py#L1147-L1163
 the equivalent logic] that `Model.save` has which could be implemented by
 doing a single query retrieving the `MAX` for each order with respect to
 values. Something like


 {{{#!python
 if order_wrt := self.model.order_with_respect_to:
    get_filter_kwargs_for_object = order_wrt.get_filter_kwargs_for_object
    attnames = get_filter_kwargs_for_object(obj[0])
    values = {
         tuple(get_filter_kwargs_for_object(obj).values())
         for obj in objs
     }
    filters = reduce(operator.or_, (
        Q(dict(zip(attnames, vals)))
        for vals in values
    ))
    max_orders = (
        self.model._base_manager.using(using)
        .values(*attnames)
        .filter(filters).
        .annotate(
            _order__max=Max("_order", default=0)
        )
    )
    max_orders_map = {max_order[:len(attnames]: max_order[-1] for max_order
 in max_orders]
    # and then assign max_orders_map to each objs
 }}}
-- 
Ticket URL: <https://code.djangoproject.com/ticket/36060#comment:1>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion visit 
https://groups.google.com/d/msgid/django-updates/010701943464546c-b62a6a3d-20e8-4b8a-b45e-974dd4635f07-000000%40eu-central-1.amazonses.com.

Reply via email to