Hello folks,

I am learning to set up multiple databases routing in Django.

Before I started, I had everything working properly. Then, I wrote 
dbrouter.py under the site directory, basically by copying the example in the 
Django document about using multiple 
databases<https://docs.djangoproject.com/en/dev/topics/db/multi-db/>. 
Here is what's in dbrouter.py:
#------------------------------------------------------------------------------------------------------------------------------------
class DBRouter(object):
    def db_for_read(self, model, **hints):
        label = model._meta.app_label
        if label == 'books':
            dbName = 'test' # dbName is a name in settings.DATABASES
        else:
            dbName = None
        return dbName

    def db_for_write(self, model, **hints):
        label = model._meta.app_label
        if label == 'books':
            dbName = 'test'
        else:
            dbName = None
        return dbName

    def allow_syncdb(self, db, model):
        # db is a string of database name.
        label = model._meta.app_label
        if label == 'books':
            dbName = 'test'
        else:
            dbName = None
        return dbName == db
#------------------------------------------------------------------------------------------------------------------------------------

As you can see from dbrouter.py, I have an app called books. 

Here is the DATABASES setting:
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': dbPath + os.sep + 'default.sqlite',
        'USER': '',
        'PASSWORD': '',
        'HOST': '',
        'PORT': '',
    },
    'test': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': dbPath + os.sep + 'test.sqlite',
        'USER': '',
        'PASSWORD': '',
        'HOST': '',
        'PORT': '',
    }
}


Then, I added the following setting to settings.py.
DATABASE_ROUTERS = [siteName + '.dbrouter.DBRouter']

Finally, I ran ./manage.py syncdb. And got these error messages:

>$ ./manage.py syncdb
Creating tables ...
Traceback (most recent call last):
  File "./manage.py", line 17, in <module>
    execute_manager(settings)
  File 
"/Users/jianbao/projects/tao.com/mysite/lib/python2.7/site-packages/django/core/management/__init__.py",
 
line 438, in execute_manager
    utility.execute()
  File 
"/Users/jianbao/projects/tao.com/mysite/lib/python2.7/site-packages/django/core/management/__init__.py",
 
line 379, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File 
"/Users/jianbao/projects/tao.com/mysite/lib/python2.7/site-packages/django/core/management/base.py",
 
line 191, in run_from_argv
    self.execute(*args, **options.__dict__)
  File 
"/Users/jianbao/projects/tao.com/mysite/lib/python2.7/site-packages/django/core/management/base.py",
 
line 220, in execute
    output = self.handle(*args, **options)
  File 
"/Users/jianbao/projects/tao.com/mysite/lib/python2.7/site-packages/django/core/management/base.py",
 
line 351, in handle
    return self.handle_noargs(**options)
  File 
"/Users/jianbao/projects/tao.com/mysite/lib/python2.7/site-packages/django/core/management/commands/syncdb.py",
 
line 109, in handle_noargs
    emit_post_sync_signal(created_models, verbosity, interactive, db)
  File 
"/Users/jianbao/projects/tao.com/mysite/lib/python2.7/site-packages/django/core/management/sql.py",
 
line 190, in emit_post_sync_signal
    interactive=interactive, db=db)
  File 
"/Users/jianbao/projects/tao.com/mysite/lib/python2.7/site-packages/django/dispatch/dispatcher.py",
 
line 172, in send
    response = receiver(signal=self, sender=sender, **named)
  File 
"/Users/jianbao/projects/tao.com/mysite/lib/python2.7/site-packages/django/contrib/auth/management/__init__.py",
 
line 30, in create_permissions
    ctype = ContentType.objects.get_for_model(klass)
  File 
"/Users/jianbao/projects/tao.com/mysite/lib/python2.7/site-packages/django/contrib/contenttypes/models.py",
 
line 38, in get_for_model
    defaults = {'name': smart_unicode(opts.verbose_name_raw)},
  File 
"/Users/jianbao/projects/tao.com/mysite/lib/python2.7/site-packages/django/db/models/manager.py",
 
line 135, in get_or_create
    return self.get_query_set().get_or_create(**kwargs)
  File 
"/Users/jianbao/projects/tao.com/mysite/lib/python2.7/site-packages/django/db/models/query.py",
 
line 378, in get_or_create
    return self.get(**lookup), False
  File 
"/Users/jianbao/projects/tao.com/mysite/lib/python2.7/site-packages/django/db/models/query.py",
 
line 344, in get
    num = len(clone)
  File 
"/Users/jianbao/projects/tao.com/mysite/lib/python2.7/site-packages/django/db/models/query.py",
 
line 82, in __len__
    self._result_cache = list(self.iterator())
  File 
"/Users/jianbao/projects/tao.com/mysite/lib/python2.7/site-packages/django/db/models/query.py",
 
line 273, in iterator
    for row in compiler.results_iter():
  File 
"/Users/jianbao/projects/tao.com/mysite/lib/python2.7/site-packages/django/db/models/sql/compiler.py",
 
line 680, in results_iter
    for rows in self.execute_sql(MULTI):
  File 
"/Users/jianbao/projects/tao.com/mysite/lib/python2.7/site-packages/django/db/models/sql/compiler.py",
 
line 735, in execute_sql
    cursor.execute(sql, params)
  File 
"/Users/jianbao/projects/tao.com/mysite/lib/python2.7/site-packages/django/db/backends/util.py",
 
line 34, in execute
    return self.cursor.execute(sql, params)
  File 
"/Users/jianbao/projects/tao.com/mysite/lib/python2.7/site-packages/django/db/backends/sqlite3/base.py",
 
line 234, in execute
    return Database.Cursor.execute(self, query, params)
django.db.utils.DatabaseError: no such table: django_content_type

Any help would be appreciated. Thank you very much.

Jim

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/django-users/-/lLX5gCYjW28J.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to