开发者

sqlalchemy and last insert id

开发者 https://www.devze.com 2023-03-04 14:39 出处:网络
req = Test() setattr(req, \'test\', 1); 开发者_如何学JAVAsession.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
                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

精彩评论

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