Search code examples
pythondjangomulti-database

django multi db change db of intern models


With multidb routers, how to move all django_* tables (models) to another db than default (moving also the django_migrations table) ?

By "move", I accept to simply put them in another db initially

EDIT : Ok, it seems that django_migration can't be moved and is present to every database...

but with that code, I'm getting a strange error :

web/settings.py:

DATABASES = {
    'intern_db': {
        'ENGINE': 'mysql.connector.django',
        'NAME': 'django_cartons',
        'USER': 'root',
        'PASSWORD' : '',
    },
    'default': {
        'ENGINE': 'mysql.connector.django',
        'NAME': 'carton2',
        'USER': 'root',
        'PASSWORD' : '',
    }
}

DATABASE_ROUTERS = ['web.AuthRouter.AuthRouter']

web/AuthRouter.py

class AuthRouter(object):
    """
    A router to control all database operations on models in the
    auth application.
    """
    def db_for_read(self, model, **hints):
        """
        Attempts to read auth models go to auth.
        """
        if model._meta.app_label == 'auth':
            return 'intern_db'
        return None

    def db_for_write(self, model, **hints):
        """
        Attempts to write auth models go to auth.
        """
        if model._meta.app_label == 'auth':
            return 'intern_db'
        return None

    def allow_relation(self, obj1, obj2, **hints):
        """
        Allow relations if a model in the auth app is involved.
        """
        if obj1._meta.app_label == 'auth' or \
         obj2._meta.app_label == 'auth':
         return True
        return None

    def allow_migrate(self, db, model):
        """
        Make sure the auth app only appears in the 'auth'
        database.
        """
        return False
        if db == 'intern_db':
            return (model._meta.app_label == 'auth')
        elif model._meta.app_label == 'auth':
            return False
        return None

Error :

$> ./manage.py migrate
Operations to perform:
  Apply all migrations: contenttypes, admin, auth, sessions
Running migrations:
  Applying contenttypes.0001_initial... OK
  Applying auth.0001_initial... OK
  Applying admin.0001_initial...Traceback (most recent call last):
  File "/usr/lib/python3.4/site-packages/mysql/connector/django/base.py", line 115, in _execute_wrapper
    return method(query, args)
  File "/usr/lib/python3.4/site-packages/mysql/connector/cursor.py", line 507, in execute
    self._handle_result(self._connection.cmd_query(stmt))
  File "/usr/lib/python3.4/site-packages/mysql/connector/connection.py", line 722, in cmd_query
    result = self._handle_result(self._send_cmd(ServerCmd.QUERY, query))
  File "/usr/lib/python3.4/site-packages/mysql/connector/connection.py", line 640, in _handle_result
    raise errors.get_exception(packet)
mysql.connector.errors.DatabaseError: 1005 (HY000): Can't create table `carton2`.`#sql-c20_10d` (errno: 150 "Foreign key constraint is incorrectly formed")

Solution

  • As said knbk, You have to pass the --database option to choose the db.

    Actually, it's a bad choice to put django tables in another db than default since other applications assume these tables are in default. moreover, there is still a "migration" table automatically created that is required by django.