【问题标题】:Using RedShift as an additional Django Database使用 RedShift 作为附加的 Django 数据库
【发布时间】:2017-04-15 02:25:35
【问题描述】:

我定义了两个数据库,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_* 表(显然由于 RedShift 不支持 postgres 类型 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".)

所以我的问题有两个:

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

相关问题

-Column 'django_migrations.id' has unsupported type 'serial' [ with Amazon Redshift]

【问题讨论】:

  • @KevinChristopherHenry 我很确定它发生在makemigrations 脚本中,即使是在一个新项目中也是如此。在后端的执行调用中,ensure_backend() 检查迁移表是否存在,如果不存在,我相信会创建它。

标签: python django postgresql orm amazon-redshift


【解决方案1】:

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

所以,回答我自己的问题:

  1. 没有。目前是不可能的。最佳解决方案是将自定义数据库路由器与只读数据库帐户结合使用,并在路由器中使用allow_migrate() 返回False
  2. 最好的解决方案是升级到 Django >= 1.10.4 并且不使用自定义数据库路由器,这样可以阻止错误。但是,如果您定义了任何其他数据库(例如只读副本),则需要注意这一点。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-09-07
    • 2015-12-19
    • 2019-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-18
    • 2019-07-28
    相关资源
    最近更新 更多