小编典典

在RedShift中使用Django多个数据库

python

我正在尝试将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


阅读 317

收藏
2021-01-20

共1个答案

小编典典

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

但是,这种方法更普遍的问题是,您真的不希望在Redshift数据库上进行Django风格的迁移请参阅[在Redshift中创建后如何更改表架构之类的内容。Redshift适用于大型数据库,更改其架构可能是一项繁重的工作。

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

2021-01-20