On 4/29/15 12:11 PM, st...@canary.md 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 <javascript:> 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
    
<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+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