简体   繁体   中英

Sqlalchemy event for bridged relationship tables

I am trying to keep track of changes to a database, and I am currently trying to do so with mapper events. This is working great for standard tables, but it does not seem to fire for bridge tables in a many-many relationship.

For example if I have these tables

# Main Classes
class TblContacts(BaseRelationsTable):

    __tablename__ = 'tbl_contacts'
    __relationstable__ = TblContactsContacts
    _linkingtables__ = [TblEventsContacts, TblEiplContacts]
    __friendlyname__ = "Contact"

    guid = Column(String(36), primary_key=True)
    created = Column(SubTimeStamp)
    modified = Column(SubTimeStamp)
    username = Column(SubString)
    contact_type = Column(SubString)
    name_first = Column(SubString)
    name_last = Column(SubString)
    job_title = Column(SubString)
    company = Column(SubString)
    phone = Column(SubPhone)
    email = Column(SubEmail)
    address_line1 = Column(SubString)
    address_line2 = Column(SubString)
    address_city = Column(SubString)
    address_state = Column(SubString)
    address_zip = Column(SubString)
    address_country = Column(SubString)

    relations = orm.relationship('TblContacts',
                                 secondary='tbl_contacts_contacts',
                                 primaryjoin='TblContacts.guid==TblContactsContacts.parent_id',
                                 secondaryjoin='TblContacts.guid==TblContactsContacts.child_id',
                                 )

class TblContactsContacts(BaseTable):
    __tablename__ = "tbl_contacts_contacts"
    __friendlyname__ = "TblContactsContacts"

    guid = Column(SubUUID, primary_key=True, nullable=False, default=uuid.uuid4)
    parent_id = Column(String, ForeignKey('tbl_contacts.guid'))
    child_id = Column(String, ForeignKey('tbl_contacts.guid'))

And these mappers applied to those tables

def _after_insert(target):
    # Do Things
    pass

def _after_update(target):
    # Do Things
    pass

def _after_delete(target):
    # Do Things
    pass

Due to simplicity of the code I would prefer to keep using after_insert, update... But I am starting to think that wont work.

Do I have to catch it with before_commit? and if so how do I sift through the records in the session to find inserted and updated records?

To wrap this question up, I did not find a way of doing exactly what I was asking in this question. I tried to grab the bridge table records from the session commit event, but that only gives you the parent table record with the relationship attributes not the object of the bridge table.

So I ended up just writing my trigger statements and adding those directly to the database. Probably a better solution in the end but it does add another place I have to edit if I add or subtract database fields.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM