Code

Opened 5 years ago

Closed 2 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 3 years ago.

Download all attachments as: .zip

Change History (9)

comment:1 Changed 5 years ago by gasull2

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

comment:2 Changed 5 years ago by gasull2

  • Cc daniel@… added; gasull@… removed

comment:3 Changed 5 years ago by Alex

  • Triage Stage changed from Unreviewed to Accepted

comment:4 Changed 5 years ago by gasull2

  • Summary changed from get_or_create should throw an exception if no lookup parameters are sent to get_or_create should throw an exception if no lookup parameters are given

Changed 3 years ago by szczav

comment:5 Changed 3 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 3 years ago by mk

  • Triage Stage changed from Accepted to 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 Changed 3 years ago by julien

  • Severity set to Normal
  • Type set to Bug

comment:8 Changed 2 years ago by aaugustin

  • Easy pickings unset
  • Resolution set to wontfix
  • Status changed from new to 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.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.