xpanta
xpanta

Reputation: 8418

Django, adding multiple databases miscofigured my settings

I had a fully working test project which used only one database (Django 1.8 + PostgreSQL 9.1). Then, I was asked to add multiple databases. I dropped the one database I was using and created 3 new ones. I modified settings.py like this:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'core',
        'USER': 'chris',
        'PASSWORD': '***',
        'HOST': 'localhost',
        'PORT': '5432',
    },
    'db1': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'db1',
        'USER': 'chris',
        'PASSWORD': '***',
        'HOST': 'localhost',
        'PORT': '5432',
        },
    'db2': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'db2',
        'USER': 'chris',
        'PASSWORD': '***',
        'HOST': 'localhost',
        'PORT': '5432',
    }
}
DEFAULT_INDEX_TABLESPACE = 'default'

(using automatic database routing and only one models.py)

When I try to make my first migrations I now get this:

django-admin.py makemigrations --database=db1

django.core.exceptions.ImproperlyConfigured: Requested setting DEFAULT_INDEX_TABLESPACE, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.

But then I do this:

(testproject) chris@ubuntu-pc ~/D/w/testproject> python manage.py shell
>>> from django.conf import settings
>>> settings.DEFAULT_INDEX_TABLESPACE
'default'
>>> settings.configure()
RuntimeError: Settings already configured.

What am I doing wrong?

Upvotes: 1

Views: 399

Answers (1)

masnun
masnun

Reputation: 11906

You should use manage.py inside the project instead of django-admin.py:

python manage.py makemigrations

You don't need to pass database names to makemigrations. But you do need to migrate each database separately while migrating.

python manage.py migrate --database=db1

Upvotes: 4

Related Questions