Here's a MVCE-style example showing the problem I have: from sqlalchemy import * from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.orm import *
Base = declarative_base() class Type(Base): __tablename__ = 'types' id = Column(Integer, primary_key=True) def __repr__(self): return '<Type {}>'.format(self.id) class Bar(Base): __tablename__ = 'bars' id = Column(Integer, primary_key=True) deleted = Column(Boolean, nullable=False, default=False) type_id = Column(ForeignKey('types.id'), nullable=True) type = relationship(Type, backref=backref('bars', primaryjoin='(Bar.type_id == Type.id) & ~Bar.deleted')) def __repr__(self): return '<Bar {} [{}, {}]>'.format(self.id, self.type, self.deleted) e = create_engine('postgresql:///test', echo=False) Base.metadata.create_all(e) s = Session(e, autoflush=False) t = Type() b1 = Bar(type=t) b2 = Bar(type=t, deleted=True) s.add(t) s.commit() s.delete(t) s.flush() So basically when I'm using the relationship in my code I never want deleted items to show up. However, for cascading I still need them. Using serverside cascades could work but if there's a way of doing that without having to switch to serverside cascades it'd be nicer. BTW the example doesn't work with SQLite, apparently it automatically NULLs invalid FKs even without specifying `on delete set null` on the FK. -- SQLAlchemy - The Python SQL Toolkit and Object Relational Mapper http://www.sqlalchemy.org/ To post example code, please provide an MCVE: Minimal, Complete, and Verifiable Example. See http://stackoverflow.com/help/mcve for a full description. --- You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+unsubscr...@googlegroups.com. To post to this group, send email to sqlalchemy@googlegroups.com. Visit this group at https://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/d/optout.