[sqlalchemy] object dirtiness from the session's perspective?
Hi All, I'm working on a variation of this recipe: http://www.sqlalchemy.org/trac/wiki/UsageRecipes/VersionedRows ...and I've got a couple of questions about changed objects: - do objects end up in session.dirty as a result of attributes being set or changed? For example: class Example(Versioned, Base): __tablename__ = 'example' id = Column(Integer, primary_key=True) data = Column(String) obj = session.query(Example).get(1) print obj.data 'something' obj.data = 'something' Is obj now considered dirty? Hopefully not, hopefully it'll only be considered dirty if the following was done: obj.data = 'something else' Would both of the above result in obj being dirty or just the latter? If both, are there any hooks for affecting this behaviour? - in a SessionExtension's before_flush method, is there any way I can tell which attributes have changed? Or, almost the same, can I check some specific attributes to see if they've changed? cheers, Chris -- Simplistix - Content Management, Batch Processing Python Consulting - http://www.simplistix.co.uk -- You received this message because you are subscribed to the Google Groups sqlalchemy group. To post to this group, send email to sqlalch...@googlegroups.com. To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en.
Re: [sqlalchemy] object dirtiness from the session's perspective?
On 7/8/2010 3:23 AM, Chris Withers wrote: Hi All, I'm working on a variation of this recipe: http://www.sqlalchemy.org/trac/wiki/UsageRecipes/VersionedRows ...and I've got a couple of questions about changed objects: - do objects end up in session.dirty as a result of attributes being set or changed? Setting an attribute is enough for the instance to wind up in Session.dirty, according to http://www.sqlalchemy.org/docs/reference/orm/sessions.html#sqlalchemy.orm.session.Session.dirty For example: class Example(Versioned, Base): __tablename__ = 'example' id = Column(Integer, primary_key=True) data = Column(String) obj = session.query(Example).get(1) print obj.data 'something' obj.data = 'something' Is obj now considered dirty? Hopefully not, hopefully it'll only be considered dirty if the following was done: So, per the docs, yes it would be dirty. I've tested this a little in the past and I believe my experience corroborated this. obj.data = 'something else' Would both of the above result in obj being dirty or just the latter? If both, are there any hooks for affecting this behaviour? - in a SessionExtension's before_flush method, is there any way I can tell which attributes have changed? Or, almost the same, can I check some specific attributes to see if they've changed? For instances that wind up in Session.dirty, you can check each for truly dirty attributes with Session.is_modified (http://www.sqlalchemy.org/docs/reference/orm/sessions.html#sqlalchemy.orm.session.Session.is_modified). That won't tell you which attributes have been modified though, only if the instance can be ignored even though it's in Session.dirty. I'm assuming if you need to know which attributes have changed then you'll have to examine the instance's state a little closer yourself, looking at the instrumented history for each attribute, etc. I've not done this though so I'm afraid that's a guess. Lance -- You received this message because you are subscribed to the Google Groups sqlalchemy group. To post to this group, send email to sqlalch...@googlegroups.com. To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en.
Re: [sqlalchemy] object dirtiness from the session's perspective?
Lance Edgar wrote: - do objects end up in session.dirty as a result of attributes being set or changed? Setting an attribute is enough for the instance to wind up in Session.dirty, according to http://www.sqlalchemy.org/docs/reference/orm/sessions.html#sqlalchemy.orm.session.Session.dirty Indeed. I'm assuming if you need to know which attributes have changed then you'll have to examine the instance's state a little closer yourself, looking at the instrumented history for each attribute, etc. I've not done this though so I'm afraid that's a guess. This turns out to be my friend: http://www.sqlalchemy.org/docs/reference/orm/mapping.html?highlight=instancestate#sqlalchemy.orm.attributes.get_history ...hence the later question about History obejcts ;-) Chris -- You received this message because you are subscribed to the Google Groups sqlalchemy group. To post to this group, send email to sqlalch...@googlegroups.com. To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en.