gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 02:11:09 30 4
gpt4 key购买 nike

<分区>

我有一个这样的模型:

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 有任何问题或直接在我的代码中..?拜托,任何帮助将不胜感激:)

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com