Postgresql Alembic无法自动转换为整型

Postgresql Alembic无法自动转换为整型,postgresql,flask-sqlalchemy,psycopg2,alembic,flask-migrate,Postgresql,Flask Sqlalchemy,Psycopg2,Alembic,Flask Migrate,我有一个这样的模型: 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 = db.Column(db.Integer, db.ForeignKey('student

我有一个这样的模型:

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 = db.Column(db.Integer, db.ForeignKey('student.id'))
然后我将
student\u id
列更改为如下所示:

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 = 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 ']
ERROR:  column "student_id" cannot be cast automatically to type integer
HINT:  You might need to specify "USING student_id::integer".
SQL state: 42804
我的软件包版本是:

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;
我收到了以下错误消息:

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 ']
ERROR:  column "student_id" cannot be cast automatically to type integer
HINT:  You might need to specify "USING student_id::integer".
SQL state: 42804
然后我尝试添加带有
提示的命令
指令:

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_和_no_name评论来了解这一点

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

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

非常感谢@a_horse\u with_no_name:)

如果要将数组(=多个值)强制转换为整数(=单个值),则需要告诉Postgres数组中的哪个元素。非常好的建议,非常感谢男士:)