#36702: bulk_create not returning pk if it was set as an expression
-------------------------------------+-------------------------------------
Reporter: Johannes Westphal | Owner: (none)
Type: Bug | Status: new
Component: Database layer | Version: 5.2
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
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
* type: Uncategorized => Bug
Comment:
Hello Jonathan, thank you for your report!
I went back to the [
https://github.com/django/django/pull/9983 change that
introduced that line] and it goes all the way back to when we added
`RETURNING` support to `bulk_create` on Postgres (see #29444).
I couldn't any find mention of ''why'' we were doing it but I assume it
was initially as form of optimization or a way to make sure the primary
key preserves its exact identity
e.g.
- `UUID('70cf7ffa-000c-4dfd-b08b-a921f09e5ff2') == UUID('70cf7ffa-000c-
4dfd-b08b-a921f09e5ff2')`
- but `UUID('70cf7ffa-000c-4dfd-b08b-a921f09e5ff2') is not UUID('70cf7ffa-
000c-4dfd-b08b-a921f09e5ff2')`
Given the ORM makes favors equality of objects and hashability of objects
over identity I think it would be safe to remove this. The `bulk_create`
test suite also doesn't complain on Postgres and SQLite so I think this is
a safe change to make, would you like to submit a PR with a test for it?
FWIW I stumbled upon this particular case when working on #36490 to avoid
unnecessary transaction in `bulk_create`.
[
https://github.com/django/django/pull/19614#issuecomment-3028756465 I
noticed] that there doesn't seem to be a reason to perform two with and
without pk partionned `INSERT` anymore.
[
https://github.com/django/django/compare/main...charettes:django:ticket-36490
-simplify-bulk_create#diff-
d58ef61559dc7af5fdf7b56fee13571a4d2948e784cd608f6afeacf3ac2fb195R805 I
demonstrated that both branches can be merged together].
--
Ticket URL: <
https://code.djangoproject.com/ticket/36702#comment:1>