Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将串行字段转换为整数的Alembic迁移_Python_Postgresql_Sqlalchemy_Alembic - Fatal编程技术网

Python 将串行字段转换为整数的Alembic迁移

Python 将串行字段转换为整数的Alembic迁移,python,postgresql,sqlalchemy,alembic,Python,Postgresql,Sqlalchemy,Alembic,我最初将我的一个SQLAlchemy模型定义为: class StreetSegment(db.Model): id = db.Column(db.Integer, autoincrement=True) # surrogate ID seg_id = db.Column(db.Integer, primary_key=True) # assigned in another system; don't increment 没有意识到seg_id将成为我的Postgres

我最初将我的一个SQLAlchemy模型定义为:

class StreetSegment(db.Model):
    id = db.Column(db.Integer, autoincrement=True)    # surrogate ID
    seg_id = db.Column(db.Integer, primary_key=True)  # assigned in another system; don't increment
没有意识到
seg_id
将成为我的Postgres数据库中的
SERIAL
字段。我真正想要的是一个带有PK约束的
整数
字段(无自动增量)。我关闭了自动增量,如下所示:

class StreetSegment(db.Model):
    id = db.Column(db.Integer, autoincrement=True)
    seg_id = db.Column(db.Integer, primary_key=True, autoincrement=False)  # <--- see here

但是这就给出了警告,自动增量和现有的自动增量只对MySQL有意义。所以我的问题是:有没有办法使用Alembic将Postgres中的
串行
转换为
整数

只需将类型显式设置为您想要的类型。这应该起作用:

op.alter_column('street_segment', 'seg_id', _type=Integer)
op.alter_column('street_segment', 'seg_id', _type=Integer)