Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/344.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python SQLAlchemy关联表与软删除_Python_Sqlalchemy - Fatal编程技术网

Python SQLAlchemy关联表与软删除

Python SQLAlchemy关联表与软删除,python,sqlalchemy,Python,Sqlalchemy,我正在尝试配置我的SQLAlchemy模型,以便为关系使用关联表。我遇到的问题是关联表有一列“removed”。如果关系已被删除(软删除),我找不到方法告诉SQLAlchemy不要返回关联对象 以下是一个例子: class A(Base): __tablename__ = "a" id = Column(Integer, primary_key=True) b_list = relationship("B", secondary=l

我正在尝试配置我的SQLAlchemy模型,以便为关系使用关联表。我遇到的问题是关联表有一列“removed”。如果关系已被删除(软删除),我找不到方法告诉SQLAlchemy不要返回关联对象

以下是一个例子:

class A(Base):
    __tablename__ = "a"
    id = Column(Integer, primary_key=True)
    b_list = relationship("B", secondary=lambda: association_table, backref="a_list")

class B(Base):
    __tablename__ = "b"
    id = Column(Integer, primary_key=True)

association_table = Table(
    "a_b_association",
    metadata,
    Column("a_id", Integer, ForeignKey(A.id), primary_key=True),
    Column("b_id", Integer, ForeignKey(B.id), primary_key=True),
    Column("removed", Integer)
)
上面的示例将始终返回关系。我需要将关系更改为仅在“removed==0”时返回

谢谢你在这方面的帮助

我知道我希望SQLAlchemy进行的SQL查询是:

SELECT A.id, B.id
FROM A
INNER JOIN a_b_association ON A.id=a_b_association.a_id
INNER JOIN B ON B.id=a_b_association.b_id
WHERE a_b_association.removed=0;

我认为您可以使用关系obj的
primaryjoin
secondaryjoin
属性,然后添加条件以仅过滤未删除的行

class A(Base):
    __tablename__ = "a"
    id = Column(Integer, primary_key=True)
    b_list = relationship(
        "B", 
        secondary=association_table,
        primaryjoin=(association_table.c.a_id == id and association_table.removed == 0)
        backref="a_list"
    )

对于ref,您可以阅读更多信息。

我认为您可以使用关系obj的
primaryjoin
secondaryjoin
属性,然后添加条件以仅过滤未删除的行

class A(Base):
    __tablename__ = "a"
    id = Column(Integer, primary_key=True)
    b_list = relationship(
        "B", 
        secondary=association_table,
        primaryjoin=(association_table.c.a_id == id and association_table.removed == 0)
        backref="a_list"
    )

有关ref,您可以在中阅读更多。

谢谢!我已经被困在这个问题上好几个小时了。我刚刚使用
secondaryjoin
参数使它工作。看起来有多种方法可以做到这一点。这目前适用于我的示例:
b\u list=relationship(“b”,secondary=lambda:association\u table,secondaryjoin=“and(association\u table.c.a\u id==b.id,association\u table.c.removed==0)”,backref=“a\u list”)
,primaryjoin用于说明将左表与关联表关联的方式,secondaryjoin用于说明将右表与关联表关联的方式。谢谢你接受答案:谢谢!我已经被困在这个问题上好几个小时了。我刚刚使用
secondaryjoin
参数使它工作。看起来有多种方法可以做到这一点。这目前适用于我的示例:
b\u list=relationship(“b”,secondary=lambda:association\u table,secondaryjoin=“and(association\u table.c.a\u id==b.id,association\u table.c.removed==0)”,backref=“a\u list”)
,primaryjoin用于说明将左表与关联表关联的方式,secondaryjoin用于说明将右表与关联表关联的方式。谢谢你接受答案:D