I would like to have a one to many relation between two ORM objects and extend this with a second relation that links to the same 'many' object while applying a constraint.
The following example may elaborate:
class Users(SQLABase):
    __tablename__ = 'users'
    id = Column(Integer, primary_key=True)
    name = Column(String)
    addresses = relationship('Addresses', backref='user')
class Addresses(SQLABase):
    __tablename__ = 'addresses'
    id = Column(Integer, primary_key=True)
    historic = (String(1))
    add1 = Column(String)
    user = Column(Integer, ForeignKey('users.id'))
I would like an attribute 'Users.valid_addresses' that relates to the same 'addresses' table filtering where Addresses.historic == 'N' like the following query:
Session.Query(Addresses).filter_by(historic = 'N').all()
I'm looking for the "SQLAlchemy way".
I have the feeling this has already been answered but I'm failing to phrase the question correctly.
This is covered in the SQLAlchemy docs under "Specifying Alternate Join Conditions".
class User(Base):
    __tablename__ = 'user'
    id = Column(Integer, primary_key=True)
    name = Column(String)
    boston_addresses = relationship("Address",
                    primaryjoin="and_(User.id==Address.user_id, "
                        "Address.city=='Boston')")
class Address(Base):
    __tablename__ = 'address'
    id = Column(Integer, primary_key=True)
    user_id = Column(Integer, ForeignKey('user.id'))
    street = Column(String)
    city = Column(String)
    state = Column(String)
    zip = Column(String)
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With