【问题标题】:Using Django Multiple Databases with RedShift将 Django 多个数据库与 RedShift 一起使用
【发布时间】:2015-12-19 14:02:26
【问题描述】:

我正在尝试使用 Django 多数据库配置,其中 MYSQL 作为我的默认数据库,redshift 作为我的分析数据库。我的配置有时看起来像这样:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'xxxx',
        'USER': 'xxxx',
        'PASSWORD': 'xxxx',
        'HOST': 'localhost',
    },
    'analytics': {
        'NAME': 'analytics',
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'USER': 'XXXXXXXXX',
        'PASSWORD': 'XXXXX',
        'HOST': 'XXXXX.us-east-1.redshift.amazonaws.com',
        'PORT': 5439,
    }
}

当我尝试使用以下命令迁移我的分析应用程序时

python manage.py migrate analytics --database analytics

我看到以下错误:

  File "/opt/envs/cinematique/bin/django-admin.py", line 5, in <module>
    management.execute_from_command_line()
  File "/opt/envs/cinematique/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
    utility.execute()
  File "/opt/envs/cinematique/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 377, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/opt/envs/cinematique/local/lib/python2.7/site-packages/django/core/management/base.py", line 288, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/opt/envs/cinematique/local/lib/python2.7/site-packages/django/core/management/base.py", line 338, in execute
    output = self.handle(*args, **options)
  File "/opt/envs/cinematique/local/lib/python2.7/site-packages/django/core/management/commands/migrate.py", line 60, in handle
    return self.show_migration_list(connection, args)
  File "/opt/envs/cinematique/local/lib/python2.7/site-packages/django/core/management/commands/migrate.py", line 308, in show_migration_list
    loader = MigrationLoader(connection, ignore_no_migrations=True)
  File "/opt/envs/cinematique/local/lib/python2.7/site-packages/django/db/migrations/loader.py", line 48, in __init__
    self.build_graph()
  File "/opt/envs/cinematique/local/lib/python2.7/site-packages/django/db/migrations/loader.py", line 183, in build_graph
    self.applied_migrations = recorder.applied_migrations()
  File "/opt/envs/cinematique/local/lib/python2.7/site-packages/django/db/migrations/recorder.py", line 59, in applied_migrations
    self.ensure_schema()
  File "/opt/envs/cinematique/local/lib/python2.7/site-packages/django/db/migrations/recorder.py", line 53, in ensure_schema
    editor.create_model(self.Migration)
  File "/opt/envs/cinematique/local/lib/python2.7/site-packages/django/db/backends/schema.py", line 270, in create_model
    self.execute(sql, params)
  File "/opt/envs/cinematique/local/lib/python2.7/site-packages/django/db/backends/schema.py", line 111, in execute
    cursor.execute(sql, params)
  File "/opt/envs/cinematique/local/lib/python2.7/site-packages/django/db/backends/utils.py", line 81, in execute
    return super(CursorDebugWrapper, self).execute(sql, params)
  File "/opt/envs/cinematique/local/lib/python2.7/site-packages/django/db/backends/utils.py", line 65, in execute
    return self.cursor.execute(sql, params)
  File "/opt/envs/cinematique/local/lib/python2.7/site-packages/django/db/utils.py", line 94, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/opt/envs/cinematique/local/lib/python2.7/site-packages/django/db/backends/utils.py", line 65, in execute
    return self.cursor.execute(sql, params)
django.db.utils.NotSupportedError: Column "django_migrations.id" has unsupported type "serial".

关于如何解决此问题的任何想法?我正在使用Django==1.7.9psycopg2==2.6.1

【问题讨论】:

    标签: python django django-models amazon-redshift


    【解决方案1】:

    具体问题是:Django想要创建一个带有serial主键的迁移管理表来跟踪迁移历史。 Redshift 不支持。

    不过,这种方法更普遍的问题是,您并不真正希望在 Redshift 数据库上进行 Django 风格的迁移(参见 How to change table schema after created in Redshift? 之类的内容)。 Redshift 适用于大型数据库,更改其架构可能是一项繁重的工作。

    所以,答案是:不要将 Django 迁移与 redshift 一起使用。 Syncdb 可以用来初始化你的表,但之后你需要手动管理模式。不要在模型用于 Redshift 的应用中创建 migrations__init__.py 文件。

    这里有相关/重复的问题:

    【讨论】:

    • 我正在尝试通过 Django ORM 将分析数据保存在 redshift 中,而不是手动编写插入。
    • 啊,我明白了。正在更新答案。
    • 更一般地说,如果您打算将 RedShift 定义为辅助 unmanaged 数据库,那么您需要定义一个带有 allow_migrate() 处理程序的自定义路由器如果使用 DB redshift,则返回 False。请参阅docs.djangoproject.com/en/1.10/topics/db/multi-db/… 了解更多信息。
    猜你喜欢
    • 2011-12-20
    • 2019-05-29
    • 2017-04-19
    • 2016-11-12
    • 1970-01-01
    • 2021-06-15
    • 2021-11-27
    • 1970-01-01
    • 2016-06-03
    相关资源
    最近更新 更多