生成的查询包含冗余产品?

Generated queries contain redundant products?

我有像这样的相当简单的模型:

TableA2TableB = Table('TableA2TableB', Base.metadata,
                        Column('tablea_id', BigInteger, ForeignKey('TableA.id')),
                        Column('tableb_id', Integer, ForeignKey('TableB.id')))


class TableA(Base):
    __tablename__ = 'TableA'
    id = Column(BigInteger, primary_key=True)
    infohash = Column(String, unique=True)
    url = Column(String)
    tablebs = relationship('TableB', secondary=TableA2TableB, backref='tableas')


class TableB(Base):
    __tablename__ = 'TableB'
    id = Column(Integer, primary_key=True)
    url = Column(String, unique=True)

但是,sqla 会生成类似

的查询
SELECT "TableB".id, "TableB".url AS "TableB_url" FROM "TableB", "TableA2TableB" 
WHERE "TableA2TableB".tableb_id = "TableB".id AND "TableA2TableB".tablea_id = 408997;

但是为什么选择的属性是TableB中的属性,查询中会有笛卡尔积? TableA2TableB 不需要。

谢谢

像现在这样,TableB(tableas)中有一个backref关系,因为默认加载模式设置为select.

所以加载了它

您可能希望将 TableA.tablebs 更改为

tablebs = relationship('TableB', secondary=TableA2TableB, backref='tableas', lazy="dynamic")