Opened 2 years ago

Closed 2 years ago

Last modified 2 years ago

#33875 closed Bug (invalid)

Inserting new record into partitioned table with triggers fails on returned values.

Reported by: David Peall Owned by: nobody
Component: Database layer (models, ORM) Version: 3.2
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

I have a PostgreSQL database with partitioned tables using inheritance and a trigger on the parent to insert into the correct child table. The trigger returns null to prevent the record from also getting inserted into the parent table.

When using Django for these tables the model.save function would fail and the solution was to use the bulk save option. However in newer versions of Django I am getting the same issue using the bulk save.

Using Model.objects.bulk_create([Model Object])

: Message.objects.bulk_create([message])
: File "/usr/local/lib/python3.8/site-packages/django/db/models/manager.py", line 85, in manager_method
: return getattr(self.get_queryset(), name)(*args, * *kwargs)
: File "/usr/local/lib/python3.8/site-packages/django/db/models/query.py", line 520, in bulk_create
: for result, field in zip(results, opts.db_returning_fields):
: TypeError: 'NoneType' object is not iterable

Using Model.save()

: message.save()
: File "/usr/local/lib/python3.8/site-packages/django/db/models/base.py", line 745, in save
: self.save_base(using=using, force_insert=force_insert,
: File "/usr/local/lib/python3.8/site-packages/django/db/models/base.py", line 782, in save_base
: updated = self._save_table(
: File "/usr/local/lib/python3.8/site-packages/django/db/models/base.py", line 888, in _save_table
: for result, field in zip(results, returning_fields):
: TypeError: 'NoneType' object is not iterable

For projects using SQL Alchemy I was able to resolve the issue using table_args = {'implicit_returning': False} in the model, I could not find the same option for Django.

Change History (2)

comment:1 by Mariusz Felisiak, 2 years ago

Resolution: invalid
Status: newclosed

Thanks for this ticket, you're setup is quite niche. You can try to subclass the built-in PostgreSQL backends and disable can_return_columns_from_insert and can_return_rows_from_bulk_insert feature flags, e.g.

from django.db.backends.postgresql import base, features

class DatabaseFeatures(features.DatabaseFeatures):
    can_return_columns_from_insert = False
    can_return_rows_from_bulk_insert = False

class DatabaseWrapper(base.DatabaseWrapper):
    features_class = DatabaseFeatures

comment:2 by David Peall, 2 years ago

Thank you the proposed solution is perfect! Got it up and running again.

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