Opened 16 years ago
Closed 13 years ago
#10993 closed Bug (wontfix)
get_or_create should throw an exception if no lookup parameters are given
Reported by: | gasull2 | Owned by: | szczav |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | 1.0 |
Severity: | Normal | Keywords: | |
Cc: | daniel@… | Triage Stage: | Design decision needed |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
Steps to reproduce: Use get_or_create with no lookup arguments but with the argument defaults.
Expected behaviour: IMHO, it should either throw an exception ("No lookup arguments" or similar) or actually create the object in the database.
What it happens instead: If you accidentally use get_or_create with no lookup arguments, it returns the the new object created with the parameter defaults, although the new object isn't saved in the database, and the boolean returned is False (indicating that the object wasn't created).
Attachments (1)
Change History (9)
comment:1 by , 16 years ago
comment:2 by , 16 years ago
Cc: | added; removed |
---|
comment:3 by , 15 years ago
Triage Stage: | Unreviewed → Accepted |
---|
comment:4 by , 15 years ago
Summary: | get_or_create should throw an exception if no lookup parameters are sent → get_or_create should throw an exception if no lookup parameters are given |
---|
by , 14 years ago
Attachment: | patch.diff added |
---|
comment:5 by , 14 years ago
Has patch: | set |
---|---|
Owner: | changed from | to
comment:6 by , 14 years ago
Triage Stage: | Accepted → Design decision needed |
---|
I'd argue that isn't a bug. Article.objects.get() does exactly the same thing. It's quite useful to call get() with no parameters from time to time, especially if the queryset has already been filtered before. The same could be said for get_or_create().
Marking as design decision needed even though Alex moved it to Accepted earlier. I think this issue could be a bit more controversial in the light of consistence between get() and get_or_create()
comment:7 by , 13 years ago
Severity: | → Normal |
---|---|
Type: | → Bug |
comment:8 by , 13 years ago
Easy pickings: | unset |
---|---|
Resolution: | → wontfix |
Status: | new → closed |
UI/UX: | unset |
#16137 argues that get_or_create should be callable without arguments, much like mk in comment 6. I think it's a better solution.
This bug still occurs in trunk.
eg. Article.objects.get_or_create(defaults={'title': 'some title'})
is trying to return all existing objects from Article model (because there are no lookup args) and when there are more objects than one raises MultipleObjectsReturned: get() returned more than one Article
Fix & tests attached - now assertion error is raised always when there aren't any lookup arguments.