我有一个带有 Integer
字段的 Flask-SQLAlchemy 模型,我想自动递增。它不是主键;这是代理ID。模型看起来像:
class StreetSegment(db.Model):
id = db.Column(db.Integer, autoincrement=True)
seg_id = db.Column(db.Integer, primary_key=True)
当我在我的 Postgres 数据库中创建表时,id
字段被创建为一个普通整数。如果我在没有为 id
指定值的情况下插入行,它不会被填充。有什么方法可以强制 SQLAlchemy 使用 SERIAL
,即使它不是主键?
最佳答案
使用Sequence
而不是 autoincrement
:
id = db.Column(db.Integer, db.Sequence("seq_street_segment_id"))
关于python - SQLAlchemy 中的整数字段不自动递增,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35897254/