#36060: IntegrityError: null value in column "_order" when bulk_create()
-------------------------------------+-------------------------------------
     Reporter:  Nikolay Fedorov      |                     Type:  Bug
       Status:  new                  |                Component:  Database
                                     |  layer (models, ORM)
      Version:  5.1                  |                 Severity:  Normal
     Keywords:  bulk_create,         |             Triage Stage:
  order_with_respect_to              |  Unreviewed
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------
 I have model with {{{order_with_respect_to}}} Meta's option targeted to
 ForeignKey. If I try to run {{{bulk_create()}}} I catch next error:
 {{{django.db.utils.IntegrityError: null value in column "_order" of
 relation "appname_modelname" violates not-null constraint}}}
 To fix this, I directly set the {{{_order}}} value for each instance in
 the list to {{{bulk_create()}}}. This is not convenient and adds
 additional code.

 {{{
 File "/Volumes/PROJECT/app/views.py", line 100, in form_valid
     product.album.bulk_create(images)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   File "/Users/user/venv/test/lib/python3.12/site-
 packages/django/db/models/manager.py", line 87, in manager_method
     return getattr(self.get_queryset(), name)(*args, **kwargs)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   File "/Users/user/venv/test/lib/python3.12/site-
 packages/django/db/models/query.py", line 818, in bulk_create
     returned_columns = self._batched_insert(
                        ^^^^^^^^^^^^^^^^^^^^^
   File "/Users/user/venv/test/lib/python3.12/site-
 packages/django/db/models/query.py", line 1875, in _batched_insert
     self._insert(
   File "/Users/user/venv/test/lib/python3.12/site-
 packages/django/db/models/query.py", line 1847, in _insert
     return query.get_compiler(using=using).execute_sql(returning_fields)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   File "/Users/user/venv/test/lib/python3.12/site-
 packages/django/db/models/sql/compiler.py", line 1836, in execute_sql
     cursor.execute(sql, params)
   File "/Users/user/venv/test/lib/python3.12/site-
 packages/django/db/backends/utils.py", line 122, in execute
     return super().execute(sql, params)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   File "/Users/user/venv/test/lib/python3.12/site-
 packages/django/db/backends/utils.py", line 79, in execute
     return self._execute_with_wrappers(
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   File "/Users/user/venv/test/lib/python3.12/site-
 packages/django/db/backends/utils.py", line 92, in _execute_with_wrappers
     return executor(sql, params, many, context)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   File "/Users/user/venv/test/lib/python3.12/site-
 packages/django/db/backends/utils.py", line 100, in _execute
     with self.db.wrap_database_errors:
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   File "/Users/user/venv/test/lib/python3.12/site-
 packages/django/db/utils.py", line 91, in __exit__
     raise dj_exc_value.with_traceback(traceback) from exc_value
   File "/Users/user/venv/test/lib/python3.12/site-
 packages/django/db/backends/utils.py", line 105, in _execute
     return self.cursor.execute(sql, params)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   File "/Users/user/venv/test/lib/python3.12/site-
 packages/psycopg/cursor.py", line 97, in execute
     raise ex.with_traceback(None)

 django.db.utils.IntegrityError: null value in column "_order" of relation
 "appname_modelname" violates not-null constraint
 }}}
-- 
Ticket URL: <https://code.djangoproject.com/ticket/36060>
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/010701942d7b317d-0b676281-d54d-4fb9-8025-3d827a48e1e9-000000%40eu-central-1.amazonses.com.

Reply via email to