使用RedShift作为额外的Django数据库

Dan*_*men 6 python django postgresql orm amazon-redshift

我定义了两个数据库,default这是一个常规的MySQL后端和redshift(使用postgres后端).我想将RedShift用作只用于django-sql-explorer的只读数据库.

这是我创建的路由器my_project/common/routers.py:

class CustomRouter(object):
    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):
        db_list = ('default', )
        if obj1._state.db in db_list and obj2._state.db in db_list:
            return True
        return None

    def allow_migrate(self, db, app_label, model_name=None, **hints):
        return db == 'default'
Run Code Online (Sandbox Code Playgroud)

我的settings.py引用如下:

DATABASE_ROUTERS = ['my_project.common.routers.CustomRouter', ]  
Run Code Online (Sandbox Code Playgroud)

调用时出现问题makemigrations,Django抛出一个错误,表明它正在尝试django_*在RedShift中创建表(并且由于serialRedShift不支持postgres类型,显然失败了:

...
raise MigrationSchemaMissing("Unable to create the django_migrations table (%s)" % exc)

django.db.migrations.exceptions.MigrationSchemaMissing: Unable to create the django_migrations table (Column "django_migrations.id" has unsupported type "serial".)
Run Code Online (Sandbox Code Playgroud)

所以我的问题是双重的:

  1. 是否可以完全禁用数据库的Django Management,但仍然使用ORM?
  2. 除了只读副本,为什么Django不认为它是一个可接受的用例来支持只读数据库?

相关问题

- 列'django_migrations.id'具有不受支持的类型'serial'[与Amazon Redshift]

Dan*_*men 5

我刚刚发现这是一个bug的结果.它已在一些PR中得到解决,最值得注意的是:https://github.com/django/django/pull/7194

那么,回答我自己的问题:

  1. 不,目前还不可能.最佳解决方案是将自定义数据库路由器与只读数据库帐户结合使用,并在路由器中allow_migrate()返回False.
  2. 最好的解决方案是升级到Django> = 1.10.4并且不使用自定义数据库路由器来停止错误.但是,如果您定义了任何其他数据库(例如只读副本),这是一个警告.