Opened 17 years ago

Closed 16 years ago

Last modified 13 years ago

#6970 closed (fixed)

misleading error message if get_or_create not provided with defaults

Reported by: James Tauber Owned by: Gary Wilson
Component: Core (Other) Version: dev
Severity: Keywords:
Cc: em@… Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: yes Patch needs improvement: yes
Easy pickings: no UI/UX: no

Description

If get_or_create is given arguments that would necessitate a create but the necessary defaults are not provided, the exception thrown is DoesNotExist: Blah matching query does not exist.

It would be more useful if the error message explained the possibility that create failed because of missing defaults.

Attachments (1)

6970.patch (577 bytes ) - added by Brantley 16 years ago.
Integrity!

Download all attachments as: .zip

Change History (11)

comment:1 by Huvet, 17 years ago

Cc: em@… added

I had a similar problem with get_or_create that took most of today to find (I'm a Django newbie). Seems as if get_or_create throws DoesNotExist errors if you get database problems creating too. I had a Foreign key contraint that failed, but that error was somehow hidden by get_or_create. Really annoying! (Tip to anyone experiencing something similar: don't use get_or_create, use the two methods separately instead).

comment:2 by bailey@…, 16 years ago

specifically in django.db.models.query, line 228

if a value passed in defaults to get_or_create would cause a null constraint violation, handled like this:

except IntegrityError, e:

return self.get(kwargs), False

guaranteed to reraise a DoesNotExist, which is not helpful in the context of get_or_create

comment:3 by Brantley, 16 years ago

Has patch: set

Yeah, this is horridly annoying. At some point, staring at the screen, I actually began to think that my Python interpreter was messed up. It seemed to not be catching an exception that it explicitly says to catch.

Anyhow, here's the patch to make some sense.

by Brantley, 16 years ago

Attachment: 6970.patch added

Integrity!

comment:4 by shawnr, 16 years ago

Owner: changed from nobody to shawnr

comment:5 by shawnr, 16 years ago

Owner: shawnr removed
Triage Stage: UnreviewedReady for checkin

I've tested this out at the DC Sprint and it seems to be working like a charm. Much nicer error message!

comment:6 by Malcolm Tredinnick, 16 years ago

milestone: 1.0

Better just to catch the generic DoesNotExist error right the end (saves a couple of variable resolutions) and there's no need to capture e2 (it's never used). They can be fixed at checkin. Other than that, this is fine and does make things clearer.

comment:7 by Malcolm Tredinnick, 16 years ago

Component: UncategorizedCore framework
Needs tests: set
Patch needs improvement: set
Triage Stage: Ready for checkinAccepted

On second thoughts, this should have tests so that we don't inadvertently break it again in the future.

comment:8 by Gary Wilson, 16 years ago

Owner: set to Gary Wilson
Status: newassigned

comment:9 by Gary Wilson, 16 years ago

Resolution: fixed
Status: assignedclosed

(In [8398]) Fixed #6970 -- Raise the original IntegrityError when all required fields aren't specified in get_or_create instead of a misleading DoesNotExist error, thanks deadwisdom.

comment:10 by Jacob, 13 years ago

milestone: 1.0

Milestone 1.0 deleted

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