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

标签 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'

我的 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".)

所以我的问题有两个方面:

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

相关问题

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

最佳答案

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

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

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

关于python - 使用 RedShift 作为额外的 Django 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40893868/

相关文章:

django - 如何排除 Django models.Model.save() 中的字段

python - 最佳实践 : prepopulate SlugField in Django (without Admin module)

python - 如何使用 python 从数据库中提取表元数据

postgresql - 为什么无符号整数在 PostgreSQL 中不可用?

python - Pybind11 默认参数 numpy 数组或 None

python - Matplotlib 的 get_ticklabels 不适用于自定义字符串标签

python - NLTK:情态频率条形图

python - 如何以相反的顺序在 pandas.core.groupby.DataFrameGroupBy 上使用 for 循环?

python - Django 的 AUTH_PROFILE_MODULE 更改登录成功 url?

node.js - 如何在 Sequelize 中创建表以使用 Node JS 存储在 Postgresql 中