On 03/16/2016 02:37 PM, Thorsten von Stein wrote:
> For several years, I have been using a pattern for making a many-to-one
> relationship from *cls* to *remoteCls* with a one-to-many backref with a
> join condition cls.foreignKey == remoteCls.id, where
> *cls* has a deletion flag _del which should exclude *cls* instances with
There are no changes that should affect the behavior of relationship in
this way. If anything, I'd wonder if the "0" value here is actually a
boolean and is interacting with some backend-specific typing behavior,
but there's not enough detail here to know.
Below is a complete test of your concept which succeeds. Please alter
this test appropriately to illustrate your failure condition occurring,
thanks!
from sqlalchemy import *
from sqlalchemy.orm import *
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
class A(Base):
__tablename__ = 'a'
id = Column(Integer, primary_key=True)
class B(Base):
__tablename__ = 'b'
id = Column(Integer, primary_key=True)
a_id = Column(ForeignKey('
a.id'))
_del = Column("del", Integer, default=0)
def make_rel(cls, remoteCls, foreignKey, backref_name):
br = backref(
backref_name,
collection_class=list,
primaryjoin=and_(
remoteCls.id == remote(getattr(cls, foreignKey)),
cls._del == 0)
)
rel = relationship(
remoteCls,
remote_side=remoteCls.id,
primaryjoin=getattr(cls, foreignKey) == remoteCls.id,
backref=br)
return rel
B.a = make_rel(B, A, "a_id", "bs")
e = create_engine("sqlite://", echo=True)
Base.metadata.create_all(e)
s = Session(e)
b1, b2, b3 = B(), B(), B()
a1 = A(bs=[b1, b2, b3])
s.add(a1)
s.commit()
b2._del = 1
s.commit()
assert
a1.bs == [b1, b3]
>
> --
> 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+...@googlegroups.com
> <mailto:
sqlalchemy+...@googlegroups.com>.
> To post to this group, send email to
sqlal...@googlegroups.com
> <mailto:
sqlal...@googlegroups.com>.
> Visit this group at
https://groups.google.com/group/sqlalchemy.
> For more options, visit
https://groups.google.com/d/optout.