[Django] #30071: Obscure error message when no default db provided

15 views
Skip to first unread message

Django

unread,
Jan 1, 2019, 4:12:45 PM1/1/19
to django-...@googlegroups.com
#30071: Obscure error message when no default db provided
--------------------------------------------+------------------------
Reporter: Benjy Weinberger | Owner: nobody
Type: Uncategorized | Status: new
Component: Uncategorized | Version: 2.1
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 1
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
--------------------------------------------+------------------------
In `ConnectionHandler`, the explicit check for the `DEFAULT_DB_ALIAS`
key in the databases dict comes only after an attempt to lookup that key.

If you have at least one non-default db, but no default db, no dummy
default will be created for you, and the `DEFAULT_DB_ALIAS` key will
not be present. Under these circumstances, referencing the `databases`
property directly will give a `KeyError` instead of the intended
`ImproperlyConfigured` error.

Worse, if you try to access a non-default db, `ensure_defaults` will
catch that `KeyError` but misinterpret it as being for the non-default
db name (which does in fact exist in the dict).

This change moves the explicit check before the attempt to use the key,
so that no `KeyError` is ever raised for `DEFAULT_DB_ALIAS`. It adds
a test for this case, and also adds a test for the case where the
default db is explicitly set to an empty dict.

--
Ticket URL: <https://code.djangoproject.com/ticket/30071>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Jan 1, 2019, 4:16:10 PM1/1/19
to django-...@googlegroups.com
#30071: Obscure error message when no default db provided
-------------------------------------+-------------------------------------
Reporter: Benjy Weinberger | Owner: Benjy
| Weinberger
Type: Uncategorized | Status: assigned
Component: Database layer | Version: 2.1
(models, ORM) |
Severity: Normal | Resolution:

Keywords: | Triage Stage:
| Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Benjy Weinberger):

* owner: nobody => Benjy Weinberger
* status: new => assigned
* component: Uncategorized => Database layer (models, ORM)


Comment:

See https://github.com/django/django/pull/10813

--
Ticket URL: <https://code.djangoproject.com/ticket/30071#comment:1>

Django

unread,
Jan 1, 2019, 4:17:57 PM1/1/19
to django-...@googlegroups.com
#30071: Obscure error message when no default db provided
-------------------------------------+-------------------------------------
Reporter: Benjy Weinberger | Owner: Benjy
| Weinberger
Type: Uncategorized | Status: assigned
Component: Database layer | Version: 2.1
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Benjy Weinberger):

Ticket branch here: https://github.com/benjyw/django/tree/ticket_30071

--
Ticket URL: <https://code.djangoproject.com/ticket/30071#comment:2>

Django

unread,
Jan 1, 2019, 4:25:26 PM1/1/19
to django-...@googlegroups.com
#30071: Obscure error message when no default db provided
-------------------------------------+-------------------------------------
Reporter: Benjy Weinberger | Owner: Benjy
| Weinberger
Type: Uncategorized | Status: assigned
Component: Database layer | Version: 2.1
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Description changed by Benjy Weinberger:

Old description:

> In `ConnectionHandler`, the explicit check for the `DEFAULT_DB_ALIAS`
> key in the databases dict comes only after an attempt to lookup that key.
>
> If you have at least one non-default db, but no default db, no dummy
> default will be created for you, and the `DEFAULT_DB_ALIAS` key will
> not be present. Under these circumstances, referencing the `databases`
> property directly will give a `KeyError` instead of the intended
> `ImproperlyConfigured` error.
>
> Worse, if you try to access a non-default db, `ensure_defaults` will
> catch that `KeyError` but misinterpret it as being for the non-default
> db name (which does in fact exist in the dict).
>
> This change moves the explicit check before the attempt to use the key,
> so that no `KeyError` is ever raised for `DEFAULT_DB_ALIAS`. It adds
> a test for this case, and also adds a test for the case where the
> default db is explicitly set to an empty dict.

New description:

In `ConnectionHandler`, the explicit check for the `DEFAULT_DB_ALIAS`
key in the databases dict comes only after an attempt to lookup that key.

If you have at least one non-default db, but no default db, no dummy
default will be created for you, and the `DEFAULT_DB_ALIAS` key will
not be present. Under these circumstances, referencing the `databases`
property directly will give a `KeyError` instead of the intended
`ImproperlyConfigured` error.

Worse, if you try to access a non-default db, `ensure_defaults` will
catch that `KeyError` but misinterpret it as being for the non-default
db name (which does in fact exist in the dict).

--

--
Ticket URL: <https://code.djangoproject.com/ticket/30071#comment:3>

Django

unread,
Jan 1, 2019, 7:15:14 PM1/1/19
to django-...@googlegroups.com
#30071: Obscure error message when no default db provided
-------------------------------------+-------------------------------------
Reporter: Benjy Weinberger | Owner: Benjy
| Weinberger
Type: Bug | Status: assigned

Component: Database layer | Version: 2.1
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted

Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Tim Graham):

* type: Uncategorized => Bug
* stage: Unreviewed => Accepted


--
Ticket URL: <https://code.djangoproject.com/ticket/30071#comment:4>

Django

unread,
Jan 9, 2019, 11:23:29 AM1/9/19
to django-...@googlegroups.com
#30071: Obscure error message when no default db provided
-------------------------------------+-------------------------------------
Reporter: Benjy Weinberger | Owner: Benjy
| Weinberger
Type: Bug | Status: closed

Component: Database layer | Version: 2.1
(models, ORM) |
Severity: Normal | Resolution: fixed
Keywords: | Triage Stage: Accepted

Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Tim Graham <timograham@…>):

* status: assigned => closed
* resolution: => fixed


Comment:

In [changeset:"222caab68a2a7345043d0c50161203cb2cfe70eb" 222caab6]:
{{{
#!CommitTicketReference repository=""
revision="222caab68a2a7345043d0c50161203cb2cfe70eb"
Fixed #30071 -- Fixed error message when a 'default' database isn't
provided.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/30071#comment:5>

Reply all
Reply to author
Forward
0 new messages