Opened 15 months ago

Last modified 6 weeks ago

#29444 assigned Cleanup/optimization

Allow fields to be part of the RETURNING clause during INSERT

Reported by: Johannes Hoppe Owned by: Johannes Hoppe
Component: Database layer (models, ORM) Version: master
Severity: Normal Keywords: django, db, returning, default, model, field
Cc: info@… Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Dependency for #27452

The SQL RETURNING statement is currently used for inserts of new objects. Mainly it is responsible to return the primary key in a single query.

That's great but currently in the postgres database backend this value is hard coded to only the primary key. I would like to propose to make this configurable.

There are plenty of uses cases for such a feature ultimately even enabling database defaults, but I would suggest to keep it very simple for now.

I would suggest to simply allow model fields to specify if they should be included in the returning statement or not. The default being not (except PK) to maintain the current behavior.

The implementation as well as the related code lines have been already discussed here:
https://github.com/django/django/pull/7525#issuecomment-354269077

I highly suggest to keep this a Postgres ONLY feature for now. That way we can see what sticks and consider extending this functionality into django.db in the future after we have seen how it's being used and gathered feedback.

Change History (19)

comment:1 Changed 15 months ago by Johannes Hoppe

Version: 2.0master

comment:2 Changed 15 months ago by Johannes Hoppe

Owner: set to Johannes Hoppe
Status: newassigned

comment:3 Changed 15 months ago by Johannes Hoppe

Has patch: set
Last edited 15 months ago by Tim Graham (previous) (diff)

comment:4 Changed 15 months ago by Johannes Hoppe

Patch needs improvement: set
Triage Stage: UnreviewedAccepted

comment:5 Changed 12 months ago by Johannes Hoppe

Patch needs improvement: unset

comment:6 Changed 12 months ago by Tim Graham

Patch needs improvement: set

comment:7 Changed 12 months ago by Johannes Hoppe

Patch needs improvement: unset
Type: New featureCleanup/optimization

comment:8 Changed 12 months ago by Tim Graham

Component: contrib.postgresDatabase layer (models, ORM)
Patch needs improvement: set
Summary: Allow modification of RETURNING values in django.contrib.postgresAllow fields to be part of the RETURNING clause during INSERT

A number of Oracle test failures remain.

comment:9 Changed 8 months ago by Johannes Hoppe

Patch needs improvement: unset

I fixed a couple of things. Nothing fails for me now locally that doesn't fail on master. Can you trigger it again?

comment:10 Changed 7 months ago by Tim Graham <timograham@…>

In b131f9c7:

Refs #29444 -- Renamed DatabaseFeatures.can_return_id* to be generic for other columns.

comment:11 Changed 7 months ago by Tim Graham

Patch needs improvement: set

You should be able to trigger the Oracle build yourself as described on wiki:Jenkins. I ran the tests locally and put the failures in a PR comment.

comment:12 Changed 6 months ago by Johannes Hoppe

Patch needs improvement: unset

comment:13 Changed 3 months ago by Carlton Gibson

Needs tests: set

comment:14 Changed 3 months ago by Johannes Hoppe

Needs tests: unset

comment:15 Changed 7 weeks ago by felixxm

Patch needs improvement: set

comment:16 Changed 7 weeks ago by Johannes Hoppe

Patch needs improvement: unset

comment:17 Changed 7 weeks ago by Mariusz Felisiak <felisiak.mariusz@…>

In bc91f27a:

Refs #29444 -- Added support for fetching a returned non-integer insert values on Oracle.

This is currently not actively used, since the ORM will ask the
SQL compiler to only return auto fields.

comment:18 Changed 7 weeks ago by felixxm

Patch needs improvement: set

comment:19 Changed 6 weeks ago by Johannes Hoppe

Patch needs improvement: unset
Note: See TracTickets for help on using tickets.
Back to Top