#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)
Change History (11)
comment:1 by , 17 years ago
Cc: | added |
---|
comment:2 by , 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 , 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.
comment:4 by , 16 years ago
Owner: | changed from | to
---|
comment:5 by , 16 years ago
Owner: | removed |
---|---|
Triage Stage: | Unreviewed → Ready 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 , 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 , 16 years ago
Component: | Uncategorized → Core framework |
---|---|
Needs tests: | set |
Patch needs improvement: | set |
Triage Stage: | Ready for checkin → Accepted |
On second thoughts, this should have tests so that we don't inadvertently break it again in the future.
comment:8 by , 16 years ago
Owner: | set to |
---|---|
Status: | new → assigned |
comment:9 by , 16 years ago
Resolution: | → fixed |
---|---|
Status: | assigned → closed |
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).