Django - command createsuperuser creates superuser in the default database when passing the --database parameter
I have three Postgre databases in my project:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'default',
'USER': 'user',
'PASSWORD': 'password',
},
'clients_db': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'clients_db',
'USER': 'user',
'PASSWORD': 'password',
},
'other': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'other',
'USER': 'user',
'PASSWORD': 'password',
},
}
And three apps: clients, users and myapp.
The app clients have a router for the clients_db database:
class ClientsRouter:
route_app_labels = {'clients'}
def db_for_read(self, model, **hints):
if model._meta.app_label in self.route_app_labels:
return 'clients_db'
return None
def db_for_write(self, model, **hints):
if model._meta.app_label in self.route_app_labels:
return 'clients_db'
return None
def allow_relation(self, obj1, obj2, **hints):
if (
obj1._meta.app_label in self.route_app_labels or
obj2._meta.app_label in self.route_app_labels
):
return True
return None
def allow_migrate(self, db, app_label, model_name=None, **hints):
if app_label in self.route_app_labels:
return db == 'clients_db'
return None
The other two apps uses the same database. I can run migrations and migrate just fine using the commands:
python manage.py makemigrations
Then, to apply migrations to the default database:
python manage.py migrate
And to apply migrations to the other database:
python manage.py migrate --database other
But when I try to run the command to create a superuser in the other database, it creates a superuser in the default database instead. The command:
python manage.py createsuperuser --database other
The installed apps:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.humanize',
'clients',
'users',
'myapp',
'django_filters',
'widget_tweaks',
]
From what I understand based on the docs (https://docs.djangoproject.com/en/4.1/ref/django-admin/#createsuperuser) the command should work and create the superuser in the specified database. Any ideas what I'm missing?
It has been some time, but since there where no other replies, I'm posting how I got around the problem posted in the question: I used shell to copy the user from the default database to the other database using these commands:
python manage.py shell
from django.contrib.auth import get_user_model
Users = get_user_model()
u = Users.objects.get(pk=1)
u.save(using='other')
The second and third lines are needed because I use a custom user model. If the standard user model is used, the commands are:
python manage.py shell
from django.contrib.auth.models import User
u = Users.objects.get(pk=1)
u.save(using='other')
Lastly, I have a multi-tenancy middleware that uses threading and I thought it was causing the issue described in the question, but for me it's weird that even using shell this happens. Anyway, this is how i solved it.