Rob*_*Rob 3 python postgresql sqlalchemy flask-sqlalchemy
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)
Run Code Online (Sandbox Code Playgroud)
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"))
Run Code Online (Sandbox Code Playgroud)