I have a one-to-one relationship between a Rule and a RuleStats object. I am trying to modify the last_updated
field on the RuleStats when any field on the Rule is updated. If they were not two separate classes it would look something like this.
class Rule(Base):
__tablename__ = 'rules'
def __init__(self, **kwargs):
Base.__init__(self, **kwargs)
self.stats = RuleStats(rule=self)
id = Column(Integer, autoincrement=True, primary_key=True)
description = Column(String, nullable=False, default='')
# ... more fields
last_updated = Column(DateTime, default=datetime.now, onupdate=datetime.now, nullable=False)
But how do I do it if I have two separate objects,
class Rule(Base):
__tablename__ = 'rules'
def __init__(self, **kwargs):
Base.__init__(self, **kwargs)
self.stats = RuleStats(rule=self)
id = Column(Integer, autoincrement=True, primary_key=True)
description = Column(String, nullable=False, default='')
# ... more fields
stats = relationship('RuleStats', uselist=False, backref='rule', cascade='all, delete-orphan')
class RuleStats(Base):
__tablename__ = 'rule_stats'
def __init__(self, **kwargs):
Base.__init__(self, **kwargs)
rule_id = Column(Integer, ForeignKey('rules.id'), primary_key=True)
last_updated = Column(DateTime, default=datetime.now, nullable=False)