I am using SQLAlchemy together with Zope transactions. My object is something like:
class MyItem(DeclarativeBase):
# ....
id = Column(Integer, primary_key=True)
# ....
then when I create a new object, and commit transaction, I would like to obtain genarated id, for example:
mi = MyItem()
DBSession.add(mi)
transaction.commit()
print mi.id # currently object is expired (not persistent with this session)
Is there some easy way to achieve this behaviour?
You can just use the default session pattern; add the object, and then flush to save the object to the database. Do not commit the transaction, let Zope handle that:
Your object will be added to the database in the current transaction, and thus the primary key will be known.