Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
344 views
in Technique[技术] by (71.8m points)

python - Using Django Multiple Databases with 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.com',
        '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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...