使用Django多重数据库与RedShift

编程入门 行业动态 更新时间:2024-10-21 19:45:50
本文介绍了使用Django多重数据库与RedShift的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我正在尝试使用MYSQL作为我的默认数据库和redshift作为我的分析数据库的Django多数据库配置。我的配置看起来有点像这样:

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,} }

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

python manage.py migrate analytics - 数据库分析

我看到以下错误:

文件/ opt / envs / cinematique / bin / dj ango-admin.py,第5行,在< module> management.execute_from_command_line()文件/opt/envs/cinematique/local/lib/python2.7/site-packages/django/core/management/__init__.py,第385行,在execute_from_command_line utility.execute()文件/opt/envs/cinematique/local/lib/python2.7/site-packages/django/core/management/__init__.py,第377行,执行 self.fetch_command(子命令).run_from_argv(self.argv)文件/opt/envs/cinematique/local/lib/python2.7/site-packages/django/core/management/base。 py,第288行,在run_from_argv self.execute(* args,** options .__ dict__)文件/opt/envs/cinematique/local/lib/python2.7/site-packages/ django / core / management / base.py,行338,执行 output = self.handle(* args,** options)文件/ opt / envs / cinematique / local / lib / python2.7 / site-packages / django / core / management / commands / migrate.py,第60行,处理 return self.show_migration_list(connection,args)文件/ opt / envs / cinematique / local / lib目录/ PY thon2.7 / site-packages / django / core / management / commands / migrate.py,第308行,在show_migration_list loader = MigrationLoader(connection,ignore_no_migrations = True)文件/ opt / envs /cinematique/local/lib/python2.7/site-packages/django/db/migrations/loader.py,第48行,__init__ self.build_graph()文件/ opt / envs /cinematique/local/lib/python2.7/site-packages/django/db/migrations/loader.py,第183行,build_graph self.applied_migrations = recorder.applied_migrations()文件 /opt/envs/cinematique/local/lib/python2.7/site-packages/django/db/migrations/recorder.py,第59行在applied_migrations中 self.ensure_schema()文件 /opt/envs/cinematique/local/lib/python2.7/site-packages/django/db/migrations/recorder.py,第53行,在ensure_schema editor.create_model(self.Migration)文件/opt/envs/cinematique/local/lib/python2.7/site-packages/django/db/backends/schema.py,第270行,在create_model self.exec ute(sql,params)文件/opt/envs/cinematique/local/lib/python2.7/site-packages/django/db/backends/schema.py,第111行,执行 cursor.execute(sql,params)文件/opt/envs/cinematique/local/lib/python2.7/site-packages/django/db/backends/utils.py,第81行,执行 return super(CursorDebugWrapper,self).execute(sql,params)文件/opt/envs/cinematique/local/lib/python2.7/site-packages/django/db/backends/utils .py,第65行,执行 return self.cursor.execute(sql,params)文件/opt/envs/cinematique/local/lib/python2.7/site-packages/django /db/utils.py,第94行,__exit__ six.reraise(dj_exc_type,dj_exc_value,traceback)文件/opt/envs/cinematique/local/lib/python2.7/site- package / django / db / backends / utils.py,第65行,执行 return self.cursor.execute(sql,params) django.db.utils.NotSupportedError:列django_migrations.id 不支持类型serial。

有关如何解决这个问题的任何想法?我使用 Django == 1.7.9 和 psycopg2 == 2.6.1

解决方案

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

尽管如此,更普遍的问题是您不希望在Redshift数据库中使用Django样式的迁移(请参阅像如何在Redshift中创建后更改表格模式?一>)。 Redshift是为巨大的数据库而设计的,改变它的模式可以是一个重量级的工作。

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

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

  • 列'django_migrations.id'具有不支持的类型'serial'[使用Amazon Redshift]
  • 错误:django_migrations.id具有不支持的类型serial与Amazon 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

I am seeing the following error:

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".

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

解决方案

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.

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.

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:

  • Column 'django_migrations.id' has unsupported type 'serial' [ with Amazon Redshift]
  • Error : django_migrations.id has unsupported type "serial" with Amazon Redshift

更多推荐

使用Django多重数据库与RedShift

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

发布评论

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

>www.elefans.com

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