开发者

sqlalchemy and last insert id

                req = Test()
                setattr(req, 'test', 1);
         开发者_如何学JAVA       session.add(req)
                print req.id

How to get a last id for req object ?


You should commit it first (after session.add(req)).


flush() is enough, it'll execute the queries and fill the auto IDs.


If you do a session.commit() before the final print statement the id attribute will be set. As written, there is no reason for the engine to perform any SQL queries, so the object has not been inserted yet. Once the query is run (on session flush or commit) the ID will be there.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜