SQLAlchemy中的整数字段不会自动递增

编程入门 行业动态 更新时间:2024-10-23 19:27:47
本文介绍了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中的整数字段不会自动递增

本文发布于:2023-10-28 18:05:53,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1537378.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:整数   字段   SQLAlchemy

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!