[Django] #25947: Query's str() method fails when default Database has no parameters

18 views
Skip to first unread message

Django

unread,
Dec 17, 2015, 11:58:20 AM12/17/15
to django-...@googlegroups.com
#25947: Query's str() method fails when default Database has no parameters
-------------------------------+--------------------
Reporter: liboz | Owner: nobody
Type: Uncategorized | Status: new
Component: Uncategorized | Version: 1.9
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------
According to the [https://docs.djangoproject.com/en/1.9/topics/db/multi-
db/ docs], we can have
{{{
default database...[with]...parameters dictionary...blank if it will not
be used.
}}}

However, when trying to print a query with something like:
{{{
print Question.objects.all().query
}}}


you get the error that
{{{
settings.DATABASES is improperly configured. Please supply the ENGINE
value.
}}}

even though the query itself can return results.

You can replicate this by creating a new project, creating a router that
routes everything to a test database like so:


{{{
class Router(object):
def db_for_read(self, model, **hints):
"""
Reads go to a randomly-chosen replica.
"""
return 'test'

def db_for_write(self, model, **hints):
"""
Writes always go to primary.
"""
return 'test'

def allow_relation(self, obj1, obj2, **hints):
"""
Relations between objects are allowed if both objects are
in the primary/replica pool.
"""
return True

def allow_migrate(self, db, app_label, model=None, **hints):
"""
All non-auth models end up in this pool.
"""
return True


# Database
# https://docs.djangoproject.com/en/1.8/ref/settings/#databases
DATABASE_ROUTERS = ['test123.settings.Router']
DATABASES = {
'default': {},
'test': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
}}}

Create a simple model like this one:


{{{
from django.db import models

# Create your models here.
class Question(models.Model):
question_text = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published')
}}}

and run the appropriate migrations on the test database.

Then attempting to print the query will fail, but the query itself will
work. I believe the error is because the sql_with_params method in
django.db.models.sql.query forces the uses of the DEFAULT_DB_ALIAS:


{{{
def sql_with_params(self):
"""
Returns the query as an SQL string and the parameters that will be
substituted into the query.
"""
return self.get_compiler(DEFAULT_DB_ALIAS).as_sql()
}}}

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

Django

unread,
Dec 17, 2015, 1:17:14 PM12/17/15
to django-...@googlegroups.com
#25947: Query's str() method fails when 'default' database is empty
-------------------------------------+-------------------------------------
Reporter: liboz | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 1.9
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by timgraham):

* needs_better_patch: => 0
* component: Uncategorized => Database layer (models, ORM)
* needs_tests: => 0
* needs_docs: => 0
* type: Uncategorized => Bug
* stage: Unreviewed => Accepted


Old description:

New description:


{{{
from django.db import models

--

Comment:

I guess this might be tricky to fix for reasons similar to this comment in
the file: "We need to use DEFAULT_DB_ALIAS here, as QuerySet does not have
(nor should it have) knowledge of which connection is going to be used."

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

Django

unread,
Jan 4, 2016, 9:51:19 AM1/4/16
to django-...@googlegroups.com
#25947: Query's str() method fails when 'default' database is empty
-------------------------------------+-------------------------------------
Reporter: liboz | Owner: nobody

Type: Bug | Status: new
Component: Database layer | Version: 1.9
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by liboz):

I looked into this a bit and Django seems to crash when running
`quote_name_unless_alias` in `django.db.models.sql.compiler` because it
uses DatabaseOperations' `quote_name` function. Could changing the
DatabaseOperations for `django.db.backends.dummy` to have a `quote_name`
function like that of sqlite?

I'm thinking of something like:
{{{
def quote_name(self, name):
if name.startswith('"') and name.endswith('"'):
return name # Quoting once is enough.
return '"%s"' % name
}}}

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

Django

unread,
Nov 30, 2021, 3:58:38 AM11/30/21
to django-...@googlegroups.com
#25947: Query's str() method fails when 'default' database is empty
-------------------------------------+-------------------------------------
Reporter: liboz | Owner: nobody

Type: Bug | Status: new
Component: Database layer | Version: 1.9
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Egor R):

* cc: Egor R (added)


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

Django

unread,
Nov 30, 2021, 6:16:50 PM11/30/21
to django-...@googlegroups.com
#25947: Query's str() method fails when 'default' database is empty
-------------------------------------+-------------------------------------
Reporter: liboz | Owner: Teddy Ni
Type: Bug | Status: assigned

Component: Database layer | Version: 1.9
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Teddy Ni):

* owner: nobody => Teddy Ni
* status: new => assigned


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

Django

unread,
Nov 30, 2021, 9:38:40 PM11/30/21
to django-...@googlegroups.com
#25947: Query's str() method fails when 'default' database is empty
-------------------------------------+-------------------------------------
Reporter: liboz | Owner: Teddy Ni
Type: Bug | Status: assigned
Component: Database layer | Version: 1.9
(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 Teddy Ni):

* has_patch: 0 => 1


Comment:

Patch is provided in https://github.com/django/django/pull/15142.

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

Django

unread,
Dec 2, 2021, 7:30:18 AM12/2/21
to django-...@googlegroups.com
#25947: Query's str() method fails when 'default' database is empty
-------------------------------------+-------------------------------------
Reporter: liboz | Owner: Teddy Ni
Type: Bug | Status: assigned
Component: Database layer | Version: 1.9
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* needs_better_patch: 0 => 1


--
Ticket URL: <https://code.djangoproject.com/ticket/25947#comment:6>

Django

unread,
Apr 1, 2024, 10:31:35 PMApr 1
to django-...@googlegroups.com
#25947: Query's str() method fails when 'default' database is empty
-------------------------------------+-------------------------------------
Reporter: liboz | Owner: jayvynl
Type: Bug | Status: assigned
Component: Database layer | Version: 1.9
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by jayvynl):

* owner: Teddy Ni => jayvynl

Comment:

I think it's reasonable to use `QuerySet.db()` to replace
`DEFAULT_DB_ALIAS` in `sql_with_db` method. To achieve this, `QuerySet`
should pass itself to `Query`, this only need just a few lines change.
--
Ticket URL: <https://code.djangoproject.com/ticket/25947#comment:7>

Django

unread,
Apr 1, 2024, 10:33:11 PMApr 1
to django-...@googlegroups.com
#25947: Query's str() method fails when 'default' database is empty
-------------------------------------+-------------------------------------
Reporter: liboz | Owner: jayvynl
Type: Bug | Status: assigned
Component: Database layer | Version: dev
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by jayvynl):

* version: 1.9 => dev

--
Ticket URL: <https://code.djangoproject.com/ticket/25947#comment:8>

Django

unread,
Apr 2, 2024, 4:22:53 AMApr 2
to django-...@googlegroups.com
#25947: Query's str() method fails when 'default' database is empty
-------------------------------------+-------------------------------------
Reporter: liboz | Owner: jayvynl
Type: Bug | Status: assigned
Component: Database layer | Version: dev
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by jayvynl):

Passing `QuseySet` to `Query` will make `QuerySet` object be evaluated
when pickling `Query` object and make pickle results big.

Related ticket #27159

It should have some reason why `Query` will be pickled. I will come up
with another approch to solve this issue.
--
Ticket URL: <https://code.djangoproject.com/ticket/25947#comment:9>

Django

unread,
Apr 2, 2024, 5:42:37 AMApr 2
to django-...@googlegroups.com
#25947: Query's str() method fails when 'default' database is empty
-------------------------------------+-------------------------------------
Reporter: liboz | Owner: jayvynl
Type: Bug | Status: assigned
Component: Database layer | Version: dev
(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 jayvynl):

* needs_better_patch: 1 => 0

--
Ticket URL: <https://code.djangoproject.com/ticket/25947#comment:10>

Django

unread,
Apr 2, 2024, 5:55:12 AMApr 2
to django-...@googlegroups.com
#25947: Query's str() method fails when 'default' database is empty
-------------------------------------+-------------------------------------
Reporter: liboz | Owner: jayvynl
Type: Bug | Status: assigned
Component: Database layer | Version: dev
(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
-------------------------------------+-------------------------------------
Comment (by jayvynl):

GitHub PR https://github.com/django/django/pull/18039
--
Ticket URL: <https://code.djangoproject.com/ticket/25947#comment:11>
Reply all
Reply to author
Forward
0 new messages