0

1. Please create a new superuser for the second/default/current database. Because you will getting this issue that i already asked before: https://gitter.im/django/django?at=5810b0b5806316005dc11f46


i use multiple site, multiple env, multiple project, multiple database. and found this error message if use DATABASE_ROUTERS.

insert or update on table “django_admin_log” violates foreign key constraint “django_admin_log_user_id_c564eba6_fk_auth_user_id” DETAIL: Key (user_id)=(1) is not present in table “auth_user”.

i already tried with this answer: http://stackoverflow.com/a/20094869/6396981 or https://code.djangoproject.com/ticket/23297, but still an error.

if i do migration with following DATABASE_ROUTERS, i found an error django.db.utils.ProgrammingError: relation "auth_user" does not exist. this why we migrated first my second database and then setup the DATABASE_ROUTERS after doing migration.


2. Please migrate first your second/default/current database before using the DATABASE_ROUTERS.

$ ./manage.py migrate
$ ./manage.py makemigrations yourapp
$ ./manage.py migrate yourapp

3. And then, after doing migration, for example in this configuration you need to implement SSO (Single Sign On), and for example in this config below, i setup it with the postgres in my yourproject/settings.py

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'db_2',
        'USER': 'db_user',
        'PASSWORD': 'db_password',
        'HOST': '127.0.0.1',
        'PORT': '5432',
    },
    'auth_db': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'db_1',
        'USER': 'db_user',
        'PASSWORD': 'db_password',
        'HOST': '127.0.0.1',
        'PORT': '5432',
    }
}
DATABASE_ROUTERS = ['yourproject.routers.DBRouter']

4. Create new file example: yourproject/routers.py, please read more about it: https://docs.djangoproject.com/en/1.9/topics/db/multi-db/#an-example

class DBRouter(object):
    """
    Docs: https://docs.djangoproject.com/en/1.9/topics/db/multi-db/#an-example
    A router to control all database operations on models in the
    auth application.
    $ ./manage.py migrate
    $ ./manage.py migrate --database=auth_db
    """

    def db_for_read(self, model, **hints):
        """
        Attempts to read auth models go to 'auth_db'.
        """
        if model._meta.app_label == 'auth':
            return 'auth_db'
        return 'default'

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

    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 'default'

    def allow_migrate(self, db, app_label, model_name=None, **hints):
        """
        Make sure the auth app only appears in the 'auth_db'
        database.
        """
        if app_label == 'auth':
            return db == 'auth_db'
        return 'default'

5. Check your superuser.

$ ./manage.py shell
>>> from django.contrib.auth.models import User
>>> User.objects.all()
[<User: user_a>, <User: user_b>, <User: user_c>, <User: user_d>]
>>>
>>> User.objects.using('auth_db')
[<User: user_a>, <User: user_b>, <User: user_c>, <User: user_d>]
>>>
>>> User.objects.using('auth_db').filter(is_superuser=True)
[<User: user_a>]
>>>
>>> User.objects.using('default')
[<User: default_db_user>]
>>> User.objects.using('default').get(username='default_db_user').is_superuser
True
>>>

So, you are require to have min 2 superuser: For first database, and second database.

But how i can save the field such as ForeignKey? Please read this previous post: How to implement the ForeignKey for multi databases Django

multi databases django problem sso settings python solution database

Your Answer

blog comments powered by Disqus