Alembic 无法自动转换为整数类型

Alembic cannot be cast automatically to type integer

我有一个这样的模型:

class Schedule(db.Model):
    __tablename__ = 'schedule'
    id = db.Column(db.Integer, primary_key=True)
    student_id = db.Column(ARRAY(db.Integer, db.ForeignKey('student.id')))

然后我把student_id列改成这样:

student_id = db.Column(db.Integer, db.ForeignKey('student.id'))

这是我的迁移文件的片段:

from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import postgresql
from sqlalchemy.dialects.postgresql import INTEGER

def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    # ...
    op.alter_column('schedule', 'student_id',
               existing_type=postgresql.ARRAY(INTEGER()),
               type_=sa.Integer(),
               existing_nullable=True)
    # ...
    # ### end Alembic commands ###

当我尝试 运行 db migrate 命令时,我收到此错误消息:

sqlalchemy.exc.ProgrammingError: (psycopg2.errors.DatatypeMismatch) column "student_id" cannot be cast automatically to type integer
HINT:  You might need to specify "USING student_id::integer".
 [SQL: 'ALTER TABLE schedule ALTER COLUMN student_id TYPE INTEGER ']

我的包版本是:

Flask==0.12
alembic==0.8.10
Flask-Migrate==2.0.3
Flask-SQLAlchemy==2.1
SQLAlchemy==1.1.5
psycopg2==2.8.3

当我尝试直接使用此命令在 SQL 上 运行 时:

ALTER TABLE schedule ALTER COLUMN student_id TYPE INTEGER;

我收到此错误消息:

ERROR:  column "student_id" cannot be cast automatically to type integer
HINT:  You might need to specify "USING student_id::integer".
SQL state: 42804

然后我尝试用HINT指令添加命令:

ALTER TABLE schedule ALTER COLUMN student_id TYPE INTEGER USING student_id::integer;

然后我得到这个错误:

ERROR:  cannot cast type integer[] to integer
LINE 1: ...ER COLUMN student_id TYPE INTEGER USING student_id::integer;
                                                             ^
SQL state: 42846
Character: 75

所以,我的问题是,这有什么问题..?Alembic 或直接在我的代码中有任何问题吗..? 拜托,任何帮助将不胜感激:)

我通过关注 @a_horse_with_no_name 评论来解决这个问题 .

所以,我的问题的答案是,我 运行 直接在 PostgreSQL 上执行以下命令:

alter table schedule alter column student_id set data type int4 using (student_id[1]::int)

非常感谢@a_horse_with_no_name:)