如何在flask-sqlalchemy orm模式下获取自动增量id

时间:2017-10-12 13:50:58

标签: python flask orm sqlalchemy flask-sqlalchemy

我使用flask sqlalchemy orm添加了记录,但是如何获取id字段?

from sqlalchemy.sql.sqltypes import Integer, String
from sqlalchemy.sql.schema import Column
from flask_sqlalchemy import SQLAlchemy

db = SQLAlchemy()

class Table(db.Model):
    __tablename__ = 'example'
    id = db.Column(Integer, primary_key=True)
    name = db.Column(String(2000))

person = Table()
person.name = 'peter'

db.session.add(person)

1 个答案:

答案 0 :(得分:0)

自动提交应设置为此事务的默认值。

ref http://docs.sqlalchemy.org/en/latest/core/connections.html#sqlalchemy.engine.Connection.execution_options.params.autocommit

http://docs.sqlalchemy.org/en/latest/core/connections.html#understanding-autocommit

假设启用了自动提交,您必须查询您的表并选择id字段。

result = session.query(person).filter_by(name='peter').first()

print result.id