在 RedShift 中使用 Django 多个数据库

编程入门 行业动态 更新时间:2024-10-20 19:01:18
本文介绍了在 RedShift 中使用 Django 多个数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

I am trying to use an Django multiple database configuration with MYSQL as my default database and redshift as my analytics database. My configuration looks sometime like this:

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', 'PORT': 5439, } }

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

When I try to migrate my analytics app, using the following command

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.9 和 psycopg2==2.6.1

Any ideas on how to resolve this issue? I am using Django==1.7.9 and psycopg2==2.6.1

推荐答案

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

The specific problem is: Django wants to create a migration-management table with a serial primary key to track migration history. Redshift doesn't support that.

不过,这种方法更普遍的问题是,您并不真的希望在 Redshift 数据库上进行 Django 风格的迁移(参见 在 Redshift 中创建后如何更改表架构?).Redshift 适用于大型数据库,更改其架构可能是一项重量级的工作.

The more general problem with the approach, though, is that you don't really want Django-style migrations on a Redshift database (see stuff like How to change table schema after created in Redshift?). Redshift is meant for huge databases and changing it's schema can be a heavyweight job.

所以,答案是:不要在 redshift 中使用 Django 迁移.Syncdb 可能没问题,可以初始化您的表,但之后您将需要手动管理架构.不要在模型适用于 Redshift 的应用中创建 migrations__init__.py 文件.

So, the answer is: don't use Django migrations with redshift. Syncdb might be ok, to initialize your tables, but after that you will need to manage the schema manually. Don't create a migrations__init__.py file in an app whose models are intended for Redshift.

此处的相关/重复问题:

Related/duplicate questions here:

  • 列django_migrations.id"有不受支持的类型串行"[使用 Amazon Redshift]
  • 错误:django_migrations.id 不受支持输入串行"使用 Amazon Redshift

更多推荐

在 RedShift 中使用 Django 多个数据库

本文发布于:2023-07-08 20:13:10,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1080250.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   数据库   RedShift   Django

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!