#33343 closed Bug (invalid)
update_or_create seems to issue wrongly limited query to the database
| Reported by: | Florian Apolloner | Owned by: | nobody | 
|---|---|---|---|
| Component: | Database layer (models, ORM) | Version: | dev | 
| Severity: | Normal | Keywords: | |
| Cc: | Triage Stage: | Unreviewed | |
| Has patch: | no | Needs documentation: | no | 
| Needs tests: | no | Patch needs improvement: | no | 
| Easy pickings: | yes | UI/UX: | no | 
Description
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.
Change History (2)
comment:1 by , 4 years ago
| Resolution: | → invalid | 
|---|---|
| Status: | new → closed | 
comment:2 by , 4 years ago
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
update_or_create()usesget()and this is an intended optimization, see #6785 and 330638b89f14e1fb06e9d313ccc9768ae167c53f.