Hi, I'm ThadeusB.

I code, I play, I love, I innovate

Getting fresh with SQLAlchemy

You have to touch the object to get its __dict__ to appear

Typically I end up having to do things non-standard when dealing with the complex business rules at work. In my most recent trial I have to take certain columns from one record, and copy them over to another record. Now this is one thing web2py made really easy. With the web2py DAL, everything is accessible by both attribute OR item (like a dictionary). Unfortunately, SQLAlchemy does not make item access as easy. The reasoning is probably its better to be explicit and define everything instead of dynamically selecting the needed columns.

Thankfully, there is a __dict__ attribute on any SQLAlchemy object instance which will return its members as a dictionary. Even better, is this dictionary is linked to the session, so if you change something in this dict, SQLAlchemy will take care of the updates on the next commit.

The problem though? Every time you commit, the __dict__ disappears. So your left with an empty dict. How do we get this back? Its simple... just touch it. I mean, all there is to do is access an attribute from your object instance after the commit, and the __dict__ will be re-populated with its data.