On 5/6/15 9:56 AM, st...@canary.md wrote:
Hmm, I don't think I could listen to the attribute event; it's saying that the AssociationProxy doesn't have "dispatch".

oh. well yes, the event has to be on the "mapped" attributes that the association proxy is proxying. the associationproxy is just a Python trick to make two object hops look like one.


Also, suppose I could detect the orphan-deletes earlier, what's the best way to suppress the objects marked as dirty?

Here's my version of your code if it helps: https://github.com/canaryhealth/sqlalchemy_audit/blob/master/sqlalchemy_audit/history_meta.py

Your help is much appreciated! Thanks very much.


On Wednesday, April 29, 2015 at 1:00:14 PM UTC-4, Michael Bayer wrote:



    On 4/29/15 12:11 PM, st...@canary.md <javascript:> wrote:
    Since my association object doesn't have extra columns, the row
    ('bob, 'apple') will be deleted. However, if there are extra
    columns, then having it marked as "dirty" is desired.

    Perhaps I would delay my recording of my audit rows until after
    orphans are resolved. I am already using "after_flush", is there
    another event I could use? Otherwise, I would need to detect an
    object as an association object without extra columns and perform
    specific logic to handle this. Is there a way to identify
    association objects?
    there's not an event in between the time that the orphan thing is
    calculated and the SQL is emitted.   After flush is a candidate if
    you can make that work.

    The approach using the event is just to make an event listener for
    this purpose and apply it to those classes that need it:


    class MyParent(Base):
        # ...

        associations = relationship("MyAssociation", cascade="all,
    delete-orphan")

    @event.listens_for(MyParent.associations, "remove")
    def on_remove(obj, child, initiator):
        object_session(obj).delete(child)


    class MyAssociation(Base):
        # ...






    Thanks!


    On Tuesday, April 28, 2015 at 7:53:21 PM UTC-4, Michael Bayer wrote:



        On 4/28/15 6:57 PM, st...@canary.md wrote:
        Hi,

        Background information: I am trying to implement
        functionality similar to the history_meta.py example
        
(http://docs.sqlalchemy.org/en/rel_0_9/_modules/examples/versioned_history/history_meta.html).
        I am listening for after_flush events and create an audit
        record and am having problems with association objects. Here
        is an example:

          class User(Auditable, self.Base, ComparableEntity):
              __tablename__ = 'usertable'
              id = Column(Integer, primary_key=True)
              name = Column(String)
              keywords = association_proxy('assocs', 'keyword')

          class Keyword(Auditable, self.Base, ComparableEntity):
              __tablename__ = 'keywordtable'
              id = Column(Integer, primary_key=True)
              word = Column(String)

          class UserKeyword(Auditable, self.Base, ComparableEntity):
              __tablename__ = 'userkeywordtable'
              user_id = Column(Integer, ForeignKey("usertable.id
        <http://usertable.id>"),
                               primary_key=True)
              keyword_id = Column(Integer,
        ForeignKey("keywordtable.id <http://keywordtable.id>"),
        primary_key=True)
              user = relationship(User,
        backref=backref("assocs",
        cascade="all, delete-orphan"))
              keyword = relationship(Keyword)
              def __init__(self, keyword=None, user=None):
                  self.user = user
                  self.keyword = keyword


          apple = Keyword(word='apple')
          pear = Keyword(word='pear')
          bob = User(name='bob')
          bob.keywords = [apple, pear]
          sess.add(bob)
          sess.commit()

          bob.keywords.remove(apple)   <====== this is when my
        question is about
          sess.commit()
        When we remove the keyword, it marks the UserKeyword
        association object is "dirty" instead of "deleted". Why is
        that? Since the row is being removed, I would expect it to
        be marked as "deleted", so that I could make an audit record
        indicating it was deleted.

        does the row actually get deleted?  the calculation of
        "orphan" isn't done until flush time, because theoretically
        you could be associating the UserKeyword to another User.

        it doesn't look like the versioned rows recipe has support
        for this use case right now.  You could force the up-front
        delete using a "remove" attribute event on that collection.


-- You received this message because you are subscribed to the
    Google Groups "sqlalchemy" group.
    To unsubscribe from this group and stop receiving emails from it,
    send an email to sqlalchemy+...@googlegroups.com <javascript:>.
    To post to this group, send email to sqlal...@googlegroups.com
    <javascript:>.
    Visit this group at http://groups.google.com/group/sqlalchemy
    <http://groups.google.com/group/sqlalchemy>.
    For more options, visit https://groups.google.com/d/optout
    <https://groups.google.com/d/optout>.

--
You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+unsubscr...@googlegroups.com <mailto:sqlalchemy+unsubscr...@googlegroups.com>. To post to this group, send email to sqlalchemy@googlegroups.com <mailto:sqlalchemy@googlegroups.com>.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to