M
M
Maxim Vasiliev2016-02-19 02:27:10
Django
Maxim Vasiliev, 2016-02-19 02:27:10

How to make migrations work with admin/auth/contenttypes on multiple db?

Available:

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'foo',
    'bar'
}

Applications 'foo' and 'bar' live in different separate databases. The entire contrib-schina should live in the local sqlite, it is needed solely because of the admin panel, and permissions do not care.
The obvious layout:
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': BASE_DIR + '/var/db.sqlite'
    },
    'foo': {
        'ENGINE': 'django.contrib.gis.db.backends.postgis',
        'HOST': 'foohost',
        'NAME': 'foo'
    },
    'bar': {
        'ENGINE': 'django.db.backends.postgresql',
        'HOST': '',
        'NAME': 'bar'
    }
}

DATABASE_ROUTERS = ['regions.dbrouter.DbRouter']   # возвращает тупо model._meta.app_label

Known issue: migrate doesn't know how to use dbrouter and puts everything either in default or in the database specified by --database. But I didn’t google a well-known solution to the problem.
If you migrate applications individually, specifying the base:
./manage.py migrate auth
./manage.py migrate admin
./manage.py migrate sessions
# к этому моменту всяческие contenttype и permissions для всех обнаруженных приложений уже созданы
./manage.py migrate foo --database=foo
# тут срабатывает post_migrate_signal и auth/contenttypes лезет в базу foo за своими таблицами...

And everything falls down with " Error creating new content types. Please make sure contenttypes "
And when you start runserver swears that You have unapplied migrations. Although everything seems to be working.
So it goes.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Vasiliev, 2016-02-19
@qmax

Workaround: disable contrib (including admin urls), do migrations, and then enable contrib back.
What threatens - it is not clear.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question