samedi 9 juillet 2016

Nullify an attribute when another attribute has been nullified

I have a model with three foreign keys that point to the same table. When one of the foreign keys is set to NULL, I also want to nullify the other two foreign keys.

primary_id = Column(Integer, ForeignKey(table.id))
secondary_id = Column(Integer, ForeignKey(table.id))
tertiary_id = Column(Integer, ForeignKey(table.id))

My initial thought was to setup an event listening for set but that will recurse indefinitely.

@event.listens_for(Model.primary_id, 'set')
@event.listens_for(Model.secondary_id, 'set')
@event.listens_for(Model.tertiary_id, 'set')
def example(target, value, old, initiator):
    target.primary_id = None
    target.secondary_id = None
    target.tertiary_id = None

Is there some way for me to stop the events propagation if I know the source is the example function provided above?

Aucun commentaire:

Enregistrer un commentaire