#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.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/048.d6416c09ba1e2e008d488f82ac0a8f15%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.