#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 Natalia Bidart):

 Following a chat with Sarah about this ticket and PR, I can confirm this
 issue is still valid. For the sake of explicitness, these are the steps to
 reproduce the original report:
 0. ensure that you have latest django installed and venv enabled if that
 applies
 1. create an empty test project `django-admin startproject projectmultidb`
 2. change into the project directory `cd projectmultidb`
 3. create a router definition, for example in
 `dbrouter.testrouter.TestRouter`, use the router as provided above (no
 `(object)` inheritance though)
 {{{#!python
 class TestRouter:

     db_alias = "test"

     def db_for_read(self, model, **hints):
         return self.db_alias

     def db_for_write(self, model, **hints):
         return self.db_alias

     def allow_relation(self, obj1, obj2, **hints):
         return True

     def allow_migrate(self, db, app_label, model_name=None, **hints):
         return True
 }}}
 4. tweak the settings to the following:
 {{{#!python
 DATABASE_ROUTERS = ["dbrouter.testrouter.TestRouter"]
 DATABASES = {
     "default": {},
     'test': {
         'ENGINE': 'django.db.backends.sqlite3',
         'NAME': BASE_DIR / 'test.sqlite3',
     },
 }
 }}}
 5. Double check no migrations are pending `python -Wall manage.py
 makemigrations` should return `No changes detected`
 6. Apply migrations from apps `python -Wall manage.py migrate
 --database=test` with output:
 {{{
 Operations to perform:
   Apply all migrations: admin, auth, contenttypes, sessions
 Running migrations:
   Applying contenttypes.0001_initial... OK
 ...
   Applying auth.0012_alter_user_first_name_max_length... OK
   Applying sessions.0001_initial... OK
 }}}
 7. Start a new app `testapp`, add a `Question` model as described above,
 generate migration, and apply it:
 {{{
 (django-3.12) [projectmultidb]$ python -Wall manage.py startapp testapp
 (django-3.12) [projectmultidb]$ python -Wall manage.py makemigrations
 Migrations for 'testapp':
   testapp/migrations/0001_initial.py
     + Create model Question
 (django-3.12) [projectmultidb]$ python -Wall manage.py migrate
 --database=test
 Operations to perform:
   Apply all migrations: admin, auth, contenttypes, sessions, testapp
 Running migrations:
   Applying testapp.0001_initial... OK
 }}}
 8. Open the shell `python -Wall manage.py shell` and run:
 {{{
 In [1]: from django.db import connections

 In [2]: from testapp.models import Question

 In [3]: Question.objects.all()
 Out[3]: <QuerySet []>

 In [4]: connections["test"].queries
 Out[4]:
 [{'sql': 'SELECT "testapp_question"."id",
 "testapp_question"."question_text", "testapp_question"."pub_date" FROM
 "testapp_question" LIMIT 21',
   'time': '0.001'}]

 In [5]: print(Question.objects.all().query)
 ---------------------------------------------------------------------------
 ImproperlyConfigured                      Traceback (most recent call
 last)
 ...
 ImproperlyConfigured: settings.DATABASES is improperly configured. Please
 supply the ENGINE value. Check settings documentation for more details.
 }}}
-- 
Ticket URL: <https://code.djangoproject.com/ticket/25947#comment:12>
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 view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/01070190a8169135-960c55f6-67e7-4e43-a877-391c15f57887-000000%40eu-central-1.amazonses.com.

Reply via email to