[Django] #33343: update_or_create seems to issue wrongly limited query to the database

3 views
Skip to first unread message

Django

unread,
Dec 7, 2021, 2:21:08 AM12/7/21
to django-...@googlegroups.com
#33343: update_or_create seems to issue wrongly limited query to the database
-------------------------------------+-------------------------------------
Reporter: Florian | Owner: nobody
Apolloner |
Type: Bug | Status: new
Component: Database | Version: dev
layer (models, ORM) |
Severity: Normal | Keywords:
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 1
UI/UX: 0 |
-------------------------------------+-------------------------------------
Just dumping this here before I forget -- haven't checked yet if this is
just the logging output or indeed a wrong query:
{{{
In [2]: User.objects.update_or_create(username='test', defaults={})
(0.000) BEGIN; args=None; alias=default
(0.000) SELECT "auth_user"."id", "auth_user"."password",
"auth_user"."last_login", "auth_user"."is_superuser",
"auth_user"."username", "auth_user"."first_name", "auth_user"."last_name",
"auth_user"."email", "auth_user"."is_staff", "auth_user"."is_active",
"auth_user"."date_joined" FROM "auth_user" WHERE "auth_user"."username" =
'test' LIMIT 21; args=('test',); alias=default
(0.000) SAVEPOINT "s140548210464576_x1"; args=None; alias=default
(0.000) INSERT INTO "auth_user" ("password", "last_login", "is_superuser",
"username", "first_name", "last_name", "email", "is_staff", "is_active",
"date_joined") SELECT '', NULL, 0, 'test', '', '', '', 0, 1, '2021-12-07
07:17:48.232962' RETURNING "auth_user"."id"; args=('', None, False,
'test', '', '', '', False, True, '2021-12-07 07:17:48.232962');
alias=default
(0.000) RELEASE SAVEPOINT "s140548210464576_x1"; args=None; alias=default
Out[2]: (<User: test>, True)
}}}
The `LIMIT 21;` here smells like the query executed for the `repr` of a
`Query` object.

--
Ticket URL: <https://code.djangoproject.com/ticket/33343>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Dec 7, 2021, 2:32:07 AM12/7/21
to django-...@googlegroups.com
#33343: update_or_create seems to issue wrongly limited query to the database
-------------------------------------+-------------------------------------
Reporter: Florian Apolloner | Owner: nobody
Type: Bug | Status: closed
Component: Database layer | Version: dev
(models, ORM) |
Severity: Normal | Resolution: invalid
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* status: new => closed
* resolution: => invalid


Comment:

`update_or_create()` uses `get()` and this is an intended optimization,
see #6785 and 330638b89f14e1fb06e9d313ccc9768ae167c53f.

--
Ticket URL: <https://code.djangoproject.com/ticket/33343#comment:1>

Django

unread,
Dec 7, 2021, 2:42:19 AM12/7/21
to django-...@googlegroups.com
#33343: update_or_create seems to issue wrongly limited query to the database
-------------------------------------+-------------------------------------
Reporter: Florian Apolloner | Owner: nobody
Type: Bug | Status: closed
Component: Database layer | Version: dev
(models, ORM) |
Severity: Normal | Resolution: invalid
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Florian Apolloner):

Ah, I mixed up queries with and without `get()`, my bad. Reminds me that I
really should fix `update_or_create` at some point to do what I expect:
Namely update first and only create as fallback. /me goes to reject a PR
that tries to introduce `update_or_create` in our codebase instead of
proper update & on conflict inserts :D

--
Ticket URL: <https://code.djangoproject.com/ticket/33343#comment:2>

Reply all
Reply to author
Forward
0 new messages