本文介绍了SQLAlchemy中的整数字段不会自动递增的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个Flask-SQLAlchemy模型,其中一个Integer字段我想自动递增.它不是主键;这是一个代理ID.该模型如下所示:
I have a Flask-SQLAlchemy model with an Integer field that I'd like to autoincrement. It's not a primary key; it's a surrogate ID. The model looks like:
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,即使它不是主键?
When I create the table in my Postgres database, the id field is created as a plain integer. If I insert rows without specifying a value for id, it doesn't get populated. Is there some way I can force SQLAlchemy to use SERIAL even if it isn't the primary key?
推荐答案使用 Sequence 而不是autoincrement:
id = db.Column(db.Integer, db.Sequence("seq_street_segment_id"))更多推荐
SQLAlchemy中的整数字段不会自动递增
发布评论