On Dec 20, 2010, at 10:00 AM, Andronikos Nedos wrote:

> Hi all,
> 
> I'm retrofitting the SA versioning example [http://www.sqlalchemy.org/
> docs/orm/examples.html#versioned-objects] into a large TG2 webapp.
> 
> Since I would like to make versioning transparent to the existing
> controller's code, I would like a new version to be emitted when I
> update a relationship. So with something like :
> Activity:
> customer_ref = Column(UUID, ForeignKey('customers.uuid'))
> customer=relationship(Customer, primaryjoin=\
> 
> Activity.__table__.c.customer_ref==Customer.__table__.c.uuid,
>                                       backref="activities")
> 
> versioning works correctly if I update the customer_ref attribute, but
> not the customer relationship. What I think is happening, is that
> since the VersionedListener operates on the before_flush hook, it
> seems that updating the customer rel will propagate the change to the
> customer_ref only during the flush, thus not creating a new version.
> 
> Is there a way around it by modifying the history mapper or do I have
> to manually add listener extensions to each relationaship ? Any other
> solutions by anyone confronted with this problem ?

The versioning example appears to be cautious about determining if there was a 
net "change" present.    The good news is that we don't actually need the "new" 
value of customer_ref when we create the historical entry, we need the 
"current" entry, which should be present in the object's dict.   Within 
create_version(), if we see no net change on column-based attributes, we assume 
nothing changed.   All we really need to do is check all the relationship() 
based attributes as well - I'll commit the attached patch which adds:

    if not obj_changed:
        # not changed, but we have relationships.  OK
        # check those too
        for prop in obj_mapper.iterate_properties:
            if isinstance(prop, RelationshipProperty) and \
                attributes.get_history(obj, prop.key).has_changes():
                obj_changed = True
                break





> 
> Many thanks and happy Xmas,
> A.
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "sqlalchemy" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to 
> [email protected].
> For more options, visit this group at 
> http://groups.google.com/group/sqlalchemy?hl=en.
> 

Attachment: versioning_relationships.patch
Description: Binary data

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.

Reply via email to