Opened 15 years ago

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

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

Download all attachments as: .zip

Change History (9)

comment:1 by gasull2, 15 years ago

comment:2 by gasull2, 15 years ago

Cc: daniel@… added; gasull@… removed

comment:3 by Alex Gaynor, 15 years ago

Triage Stage: UnreviewedAccepted

comment:4 by gasull2, 15 years ago

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

by szczav, 13 years ago

Attachment: patch.diff added

comment:5 by szczav, 13 years ago

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 by Matthias Kestenholz, 13 years ago

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 by Julien Phalip, 13 years ago

Severity: Normal
Type: Bug

comment:8 by Aymeric Augustin, 12 years ago

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