Opened 2 days ago
Last modified 2 hours ago
#36060 assigned Bug
IntegrityError: null value in column "_order" when bulk_create()
Reported by: | Nikolay Fedorov | Owned by: | myoungjinGo |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | 5.1 |
Severity: | Normal | Keywords: | bulk_create, order_with_respect_to |
Cc: | Triage Stage: | Accepted | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
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
Change History (3)
comment:1 by , 16 hours ago
Triage Stage: | Unreviewed → Accepted |
---|
comment:3 by , 2 hours ago
Owner: | set to |
---|---|
Status: | new → assigned |
Note:
See TracTickets
for help on using tickets.
This is effectively not something
bulk_create
currently implements.It's missing the equivalent logic that
Model.save
has which could be implemented by doing a single query retrieving theMAX
for each order with respect to values. Something like