当我尝试“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?
最佳答案
我遇到了同样的问题;这是 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% 对齐。
关于python - 如何使用 SQLAlchemy 和 Alembic 在 Python 中使用 Redshift 方言进行迁移?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27409727/