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.
精彩评论