1

I want to use the users from a legacy database but keep everything else on the 'default' database.

The main issue I have atm is that I can't get the database router to properly forward queries to the appropriate database. Namely when I run the migrations 2 times, the second time I get an error

~> DJANGO_SETTINGS_MODULE=settings python manage.py makemigrations
Migrations for 'myapp':
  myapp/migrations/0001_initial.py
    - Create model CustomUser
~> DJANGO_SETTINGS_MODULE=settings python manage.py makemigrations
Traceback (most recent call last):
  File "manage.py", line 21, in <module>
    main()
  File "manage.py", line 17, in main
    execute_from_command_line(sys.argv)
  File "/home/manos/workspace/umed/common/oauth2_test/.env/lib/python3.7/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
    utility.execute()
  File "/home/manos/workspace/umed/common/oauth2_test/.env/lib/python3.7/site-packages/django/core/management/__init__.py", line 395, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/manos/workspace/umed/common/oauth2_test/.env/lib/python3.7/site-packages/django/core/management/base.py", line 328, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/home/manos/workspace/umed/common/oauth2_test/.env/lib/python3.7/site-packages/django/core/management/base.py", line 369, in execute
    output = self.handle(*args, **options)
  File "/home/manos/workspace/umed/common/oauth2_test/.env/lib/python3.7/site-packages/django/core/management/base.py", line 83, in wrapped
    res = handle_func(*args, **kwargs)
  File "/home/manos/workspace/umed/common/oauth2_test/.env/lib/python3.7/site-packages/django/core/management/commands/makemigrations.py", line 101, in handle
    loader.check_consistent_history(connection)
  File "/home/manos/workspace/umed/common/oauth2_test/.env/lib/python3.7/site-packages/django/db/migrations/loader.py", line 299, in check_consistent_history
    connection.alias,
django.db.migrations.exceptions.InconsistentMigrationHistory: Migration admin.0001_initial is applied before its dependency myapp.0001_initial on database 'auth_db'.

So I assume it tries to run the admin stuff inside the auth_db instead of default database. But I can't really figure out what I'm doing wrong since the routing seems correct.

Simplified code below

models.py

class CustomUser(AbstractUser):
    password = models.CharField(max_length=128)
    username = models.CharField(unique=True, max_length=32)

    class Meta:
        managed = False
        db_table = 'myauthdb_user'

    objects = UserManager()

db.py

class AuthRouter:
    """ Forwards queries for users models to the auth database. """

    def db_for_read(self, model, **hints):
        if model.__name__ == 'CustomUser':
            return 'auth_db'
        return None

settings.py

DATABASE_ROUTERS = ['db.AuthRouter']

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': join(BASE_DIR, 'db.app'),
    },
    'auth_db': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'postgres',
        'USER': 'postgres',
        'PASSWORD': 'postgres',
        'HOST': 'localhost',
    },
}

I can use the ORM just fine e.g. CustomUser.objects.all(). But the migration issue is a huge problem. A naive solution is to disable the admin but I feel the underlying problem is still there.

Pithikos
  • 18,827
  • 15
  • 113
  • 136

0 Answers0