Opened 8 years ago

Closed 8 years ago

Last modified 8 years ago

#27285 closed Uncategorized (duplicate)

Multi-db example : default database config can not be an empty dict

Reported by: Jose M Herrero Owned by: nobody
Component: Documentation Version: 1.10
Severity: Normal Keywords: multi-db dbrouter
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Jose M Herrero)

I have the same problem as ticket #26955 that is closed, documentation https://docs.djangoproject.com/es/1.10/topics/db/multi-db/ says default database can be left empty but then tests fails.
To reproduce with nothing interfering I started a fresh project, these are the steps to reproduce:

  1. Start a new Django 1.10.1 project with django-admin startproject mysite and cd into it cd mysite
  2. Create a database router mysite/dbrouter.py

    Code highlighting:

    class DbRouter(object):
        def db_for_read(self, model, **hints):
            return 'master'
    
        def db_for_write(self, model, **hints):
            return 'master'
    
        def allow_relation(self, obj1, obj2, **hints):
            return True
    
        def allow_migrate(self, db, app_label, model_name=None, **hints):
            return True
    
  1. Edit mysite.settings.py as doc says with empty default and set the database router

Code highlighting:

DATABASES = {
    'default': {},
    'master': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    },
}
DATABASE_ROUTERS = ['mysite.dbrouter.DbRouter']
  1. Create mysite/tests.py

    Code highlighting:

    from django.test import TestCase
    
    class SurveyFormTest(TestCase):
        def test_sample(self):
            print 'hello'
    
  2. Execute the tests and bang! python manage.py test mysite
Creating test database for alias 'master'...
hello
.E
======================================================================
ERROR: test_sample (mysite.tests.SurveyFormTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/chemary/workspace/myvirtualenv/local/lib/python2.7/site-packages/django/test/testcases.py", line 216, in __call__
    self._post_teardown()
  File "/home/chemary/workspace/myvirtualenv/local/lib/python2.7/site-packages/django/test/testcases.py", line 908, in _post_teardown
    self._fixture_teardown()
  File "/home/chemary/workspace/myvirtualenv/local/lib/python2.7/site-packages/django/test/testcases.py", line 1060, in _fixture_teardown
    return super(TestCase, self)._fixture_teardown()
  File "/home/chemary/workspace/myvirtualenv/local/lib/python2.7/site-packages/django/test/testcases.py", line 943, in _fixture_teardown
    inhibit_post_migrate=inhibit_post_migrate)
  File "/home/chemary/workspace/myvirtualenv/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 130, in call_command
    return command.execute(*args, **defaults)
  File "/home/chemary/workspace/myvirtualenv/local/lib/python2.7/site-packages/django/core/management/base.py", line 345, in execute
    output = self.handle(*args, **options)
  File "/home/chemary/workspace/myvirtualenv/local/lib/python2.7/site-packages/django/core/management/commands/flush.py", line 54, in handle
    allow_cascade=allow_cascade)
  File "/home/chemary/workspace/myvirtualenv/local/lib/python2.7/site-packages/django/core/management/sql.py", line 15, in sql_flush
    tables = connection.introspection.django_table_names(only_existing=True, include_views=False)
  File "/home/chemary/workspace/myvirtualenv/local/lib/python2.7/site-packages/django/db/backends/base/introspection.py", line 88, in django_table_names
    existing_tables = self.table_names(include_views=include_views)
  File "/home/chemary/workspace/myvirtualenv/local/lib/python2.7/site-packages/django/db/backends/base/introspection.py", line 55, in table_names
    with self.connection.cursor() as cursor:
  File "/home/chemary/workspace/myvirtualenv/local/lib/python2.7/site-packages/django/db/backends/base/base.py", line 233, in cursor
    cursor = self.make_cursor(self._cursor())
  File "/home/chemary/workspace/myvirtualenv/local/lib/python2.7/site-packages/django/db/backends/dummy/base.py", line 21, in complain
    raise ImproperlyConfigured("settings.DATABASES is improperly configured. "
ImproperlyConfigured: settings.DATABASES is improperly configured. Please supply the ENGINE value. Check settings documentation for more details.

----------------------------------------------------------------------
Ran 1 test in 0.002s

FAILED (errors=1)
Destroying test database for alias 'master'...

Change History (6)

comment:1 by Jose M Herrero, 8 years ago

Description: modified (diff)

comment:2 by Tim Graham, 8 years ago

Does it make a difference if DbRouter.allow_migrate() returns False for 'default'?

comment:3 by Jose M Herrero, 8 years ago

It does no make any difference, I verified that Django was calling allow_migrate.

comment:4 by Tim Graham, 8 years ago

What about adding multi_db=True to the SurveyFormTest?

comment:5 by Tim Graham, 8 years ago

Component: DocumentationTesting framework
Resolution: duplicate
Status: newclosed
Type: UncategorizedBug

Duplicate of #25504.

comment:6 by Jose M Herrero, 8 years ago

Component: Testing frameworkDocumentation
Type: BugUncategorized

Same problem with multi_db=True. Sorry for issuing a duplicate ticket, I didn't found it before.

Note: See TracTickets for help on using tickets.
Back to Top