我定义了两个数据库,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'
我的 settings.py
是这样引用它的:
DATABASE_ROUTERS = ['my_project.common.routers.CustomRouter', ]
问题发生在调用makemigrations
时,Django 抛出一个错误,指示它正在尝试在 RedShift 中创建 django_*
表(显然失败是因为 postgres RedShift 不支持 serial
类型:
...
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".)
所以我的问题有两个方面:
- 是否可以完全禁用数据库的 Django 管理,但仍使用 ORM?
- 除了只读副本,为什么 Django 不认为它是支持只读数据库的可接受用例?
相关问题
- Column 'django_migrations.id' has unsupported type 'serial' [ with Amazon Redshift]
最佳答案
我刚刚发现这是 bug 的结果.它已在一些 PR 中得到解决,最值得注意的是:https://github.com/django/django/pull/7194
所以,回答我自己的问题:
- 没有。目前不可能。最好的解决方案是将自定义数据库路由器与只读数据库帐户结合使用,并让
allow_migrate()
在路由器中返回False
。 - 最好的解决方案是升级到 Django >= 1.10.4 并且不使用自定义数据库路由器,这样可以阻止错误。但是,如果您定义了任何其他数据库,例如只读副本,则需要注意这一点。
关于python - 使用 RedShift 作为额外的 Django 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40893868/