首页 > 解决方案 > 更改 django_migrations 架构

问题描述

有什么方法可以更改django_migrations表的架构?我想将所有与 Django 相关的表移动到另一个名为 的模式django,我已经使用过数据库路由器

    class DjangoAppRouter:
        route_app_labels = {'auth', 'contenttypes', 'admin'}

        def db_for_read(self, model, **hints):
            if model._meta.app_label in self.route_app_labels:
                return 'django'
            return None

        def db_for_write(self, model, **hints):
            if model._meta.app_label in self.route_app_labels:
                return 'django'
            return None

        def allow_relation(self, obj1, obj2, **hints):
            return True

        def allow_migrate(self, db, app_label, model_name=None, **hints):
            if app_label in self.route_app_labels:
                return db == 'django'
            return None


    class PrimaryRouter:
        def db_for_read(self, model, **hints):
            return 'default'

        def db_for_write(self, model, **hints):
            return 'default'

        def allow_relation(self, obj1, obj2, **hints):
            return True

        def allow_migrate(self, db, app_label, model_name=None, **hints):
            return True

设置.py:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': ENV('DB_NAME'),
        'USER': ENV('DB_USER'),
        'PASSWORD': ENV('DB_PASS'),
        'HOST': ENV('DB_HOST'),
        'PORT': ENV.int('DB_PORT'),
    },
    'django': {
        'ENGINE': 'django.db.backends.postgresql',
        'OPTIONS': {
            'options': '-c search_path=django'
        },
        'NAME': ENV('DB_NAME'),
        'USER': ENV('DB_USER'),
        'PASSWORD': ENV('DB_PASS'),
        'HOST': ENV('DB_HOST'),
        'PORT': ENV.int('DB_PORT'),
    }
}

DATABASE_ROUTERS = ['bilikStudio.db-router.DjangoAppRouter', 'bilikStudio.db-router.PrimaryRouter']

但这不起作用django_migrations。有什么建议么?使用此配置,它给了我这个错误:

django.db.migrations.exceptions.MigrationSchemaMissing: Unable to create the django_migrations table (no schema has been selected to create in
LINE 1: CREATE TABLE "django_migrations" ("id" serial NOT NULL PRIMA...

标签: djangopostgresqldjango-models

解决方案


推荐阅读