How make 'modified' flag propagate from a related object to parent

42 views
Skip to first unread message

Adam Dziendziel

unread,
Jul 27, 2008, 4:06:32 PM7/27/08
to sqlalchemy
Hello,

I have an Entity that has attribute which refers to an ID of a XML
document stored as described here:
http://www.sqlalchemy.org/trac/browser/sqlalchemy/trunk/examples/elementtree/optimized_al.py

Entity has a 'modified' attribute, which stores time of last
modification of the object. This attribute is normally updated in a
mapper extension's before_insert and before_update handlers.

However, changes to the XML document don't mark the instance of Entity
modified, Entity's mapper extension is not called and modification
time is not updated. Is there an option or workaround to make the
modification marker propagate from a related object to a parent
instance?

Best regards,
Adam

Michael Bayer

unread,
Jul 27, 2008, 4:43:33 PM7/27/08
to sqlal...@googlegroups.com

Without seeing more detail, my first impression looks like this:

class SomeXMLObject(object):
def modified(self):
return self._modified
def set_modified(self, x):
self._modified = x
self.parent.modified = x
modified = property(modified, set_modified)

The related object would need to explicitly propagate to its parent
object.

Adam Dziendziel

unread,
Jul 27, 2008, 5:16:01 PM7/27/08
to sqlalchemy
On 27 Lip, 22:43, Michael Bayer <mike...@zzzcomputing.com> wrote:
>
> The related object would need to explicitly propagate to its parent
> object.

My 'modified' is a column which stores time. It should get updated
when an instance is modified (dirty). I am populating this column in a
session extension. However, changes made to a contained XML document
doesn't make container object dirty and the session extension
before_insert/update is not called.

Anyway, in my application many different classes contains XML data.
Simple 'parent' backref won't work, because it would be ambiguous to
which class it refers (unless I use a common base class for all
entities which contains XML).

Michael Bayer

unread,
Jul 27, 2008, 7:16:46 PM7/27/08
to sqlal...@googlegroups.com

On Jul 27, 2008, at 5:16 PM, Adam Dziendziel wrote:

>
> On 27 Lip, 22:43, Michael Bayer <mike...@zzzcomputing.com> wrote:
>>
>> The related object would need to explicitly propagate to its parent
>> object.
>
> My 'modified' is a column which stores time. It should get updated
> when an instance is modified (dirty). I am populating this column in a
> session extension. However, changes made to a contained XML document
> doesn't make container object dirty and the session extension
> before_insert/update is not called.

SessionExtension doesn't have a before_insert/update method, do you
mean MapperExtension ?

> Anyway, in my application many different classes contains XML data.
> Simple 'parent' backref won't work, because it would be ambiguous to
> which class it refers (unless I use a common base class for all
> entities which contains XML).

as long as the parent is using relation() to the child, the child is
capable of having a backref. If the child is capable of being
attached to many kinds of parent, then it would have many kinds of
backref - pick the non-None one to find the path to the parent.

Since you need to change the flush plan before it happens, the
SessionExtension before_flush() is the only place you can hook this
operation in implicitly. Scan for dirty objects of the appropriate
type and then mark their parent object dirty. The work you do within
before_flush() can replace the before_insert()/before_update() hook
you're using.

Adam Dziendziel

unread,
Jul 28, 2008, 1:22:28 AM7/28/08
to sqlalchemy
On 28 Lip, 01:16, Michael Bayer <mike...@zzzcomputing.com> wrote:
> On Jul 27, 2008, at 5:16 PM, Adam Dziendziel wrote:
>
> SessionExtension doesn't have a before_insert/update method, do you
> mean MapperExtension ?

Yes, my mistake.

> Since you need to change the flush plan before it happens, the
> SessionExtension before_flush() is the only place you can hook this
> operation in implicitly. Scan for dirty objects of the appropriate
> type and then mark their parent object dirty. The work you do within
> before_flush() can replace the before_insert()/before_update() hook
> you're using.

Thank you!
Reply all
Reply to author
Forward
0 new messages