Opened 10 years ago

Closed 7 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


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)

patch.diff (1.4 KB) - added by szczav 8 years ago.

Download all attachments as: .zip

Change History (9)

comment:1 Changed 10 years ago by gasull2

comment:2 Changed 10 years ago by gasull2

Cc: daniel@… added; gasull@… removed

comment:3 Changed 9 years ago by Alex Gaynor

Triage Stage: UnreviewedAccepted

comment:4 Changed 9 years ago by gasull2

Summary: get_or_create should throw an exception if no lookup parameters are sentget_or_create should throw an exception if no lookup parameters are given

Changed 8 years ago by szczav

Attachment: patch.diff added

comment:5 Changed 8 years ago by szczav

Has patch: set
Owner: changed from nobody to szczav

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.

comment:6 Changed 8 years ago by Matthias Kestenholz

Triage Stage: AcceptedDesign 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 Changed 8 years ago by Julien Phalip

Severity: Normal
Type: Bug

comment:8 Changed 7 years ago by Aymeric Augustin

Easy pickings: unset
Resolution: wontfix
Status: newclosed
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.

Note: See TracTickets for help on using tickets.
Back to Top