子供のフィルタは機能しません。私は間違った方法で何が行われているのか分からない。Flask sqlalchemy - 多対多の関係 - 子レベルのフィルタリング
country.py
product_country = Table('product_country', Base.metadata,
Column('product_id', Integer, ForeignKey('product.id'), primary_key=True),
Column('country_id', Integer, ForeignKey('country.id'), primary_key=True)
)
class Country(Base):
__tablename__="country"
id = Column(Integer, primary_key=True)
name = Column(String(200))
products = relationship(Product, secondary=product_country, backref='countries')
product.py
class Product(Base):
__tablename__="product"
id = Column(Integer, primary_key=True)
color = Column(Integer)
....
は、SQLAlchemyの検索:
country = s.query(Country).join(Country.products).filter(Country.id==1).filter(Product.color==1).first()
まあ、私はID = 1で国を取得し、私が何をしたいものですしかし、country.productsのリストでは、color = 1の商品しか期待していませんが、国に割り当てられているすべての商品があります。あなたは私を助けてくれますか?ありがとうございます
'Product'のより完全なスニペットを投稿できますか?' Product.color'の定義方法はわかりません。 – bgse
@bgse - 色が今定義されているProductクラスを更新しました。しかし、それは色の定義についてではありません。あなたは、sqlalchemyのクエリでidで色を置き換えることができます。同じ問題があります。 – user4206969