【问题标题】:How can I get a Redshift dialect working in Python with SQLAlchemy and Alembic for migrations?如何使用 SQLAlchemy 和 Alembic 在 Python 中使用 Redshift 方言进行迁移?
【发布时间】:2015-02-09 03:53:36
【问题描述】:

当我尝试“alembic upgrade +1”时出现以下错误,这是为了进行数据库迁移。

File "/home/jason/redshift/env/lib/python3.4/site-packages/sqlalchemy/engine/default.py", line 436, in do_execute
    cursor.execute(statement, parameters)
psycopg2.NotSupportedError: Column "analyses.id" has unsupported type "serial".

我的 SQLAlchemy models.py 包含:

from alembic.ddl.postgresql import PostgresqlImpl
class RedshiftImpl(PostgresqlImpl):
    __dialect__ = 'redshift'

我的网址/主机是:

sqlalchemy.url = redshift://USER:PASSWORD@XXXX.XXXXXXX.us-east-1.redshift.amazonaws.com:5439/

但由于某种原因,它似乎没有使用这种方言。我需要修改 env.py 吗?还是models.py?

【问题讨论】:

  • 请注意 - 您的红移密码已暴露在该网址中。

标签: python sqlalchemy amazon-redshift alembic


【解决方案1】:

我遇到了同样的问题;这是 0.4.1 中的一个错误,这是本文发布时 PyPI 上可用的最新版本。我直接从 Github 安装它并且它工作正常(在撰写本文时,master 在 0.5.1a 上)。 https://github.com/binarydud/redshift_sqlalchemy

问题在于主键的处理。如果您通过 pip 安装并生成迁移,则问题出在 PrimarykeyConstraint() 方法中,该方法将要创建一个串行列,该列仅存在于 postgres 中,而不存在于 redshift 中。

from alembic import op
import sqlalchemy as sa
from redshift_sqlalchemy import dialect as postgresql

def upgrade():
    op.create_table('mytable',
        sa.Column('id', sa.Integer(), nullable=False),
        sa.PrimaryKeyConstraint('id') # problem is here: wants to create a serial type
    )

还可以考虑更改迁移以从 redshift_sqlalchemy 模块而不是 sqlalchemy 核心导入方言。尽管在此示例中无关紧要,但在某些极端情况下,redshift 和 postgres 方言并非 100% 对齐。

【讨论】:

  • 如何从 redshift_alchemy 导入方言?
  • 从github安装命令是pip install git+https://github.com/binarydud/redshift_sqlalchemy
猜你喜欢
  • 2018-12-15
  • 2018-06-29
  • 1970-01-01
  • 1970-01-01
  • 2012-11-20
  • 2014-12-02
  • 2016-12-25
  • 2017-07-12
  • 2017-05-11
相关资源
最近更新 更多