Created
January 28, 2014 03:06
-
-
Save deontologician/8661657 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Tracker(Base): | |
__tablename__ = 'tracker' | |
id = Column(Integer, primary_key=True) | |
state = Column(String, nullable=False) | |
def __repr__(self): | |
return 'Tracker<{.id}>'.format(self) | |
class Child(Base): | |
__tablename__ = 'child' | |
__table_args__ = {'sqlite_autoincrement': True} | |
id = Column(Integer, primary_key=True) | |
parent_id = Column(Integer, ForeignKey('parent.id')) | |
tracker = relationship(Tracker, | |
primaryjoin=(id == Tracker.id), | |
foreign_keys=(id,), | |
innerjoin=True, | |
uselist=False) | |
def __repr__(self): | |
return 'Child<{.id}>'.format(self) | |
@hybrid_property | |
def state(self): | |
return self.tracker.state | |
@state.setter | |
def state(self, value): | |
self.tracker.state = value | |
@state.expression | |
def state(cls): | |
return Tracker.state | |
class Parent(Base): | |
__tablename__ = 'parent' | |
__table_args__ = {'sqlite_autoincrement': True} | |
id = Column(Integer, primary_key=True) | |
has_stopped_children1 = column_property( | |
exists() | |
.where(Child.parent_id == id) | |
.where(Child.state == 'stop') | |
.correlate_except(Tracker) | |
.label('has_stopped_children'), | |
) | |
@hybrid_property | |
def has_stopped_children2(self): | |
return any(child.state == 'stop' for child in self.children) | |
@has_stopped_children2.expression | |
def has_stopped_children2(cls): | |
return cls.children.any(Child.state == 'stop') | |
children = relationship(Child, backref='parent') | |
def __repr__(self): | |
return 'Parent<{.id}>'.format(self) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment