Hi devs,
I have one problem related to join and adding conditions (sorry for
misleading subject, I don't know how to describe it shortly).
I have two tables:
Product
Tag
There is many to many relation between tag and products and I need to
select product where two tags are set. How to do this using
SqlAlchemy?
Here are table definitions:
====================
ProductTable = sql.Table(
"Product", meta.metadata,
sql.Column("productId" , sql.Integer , primary_key=True),
... (not important)
)
TagTable = sql.Table(
"Tag", meta.metadata,
sql.Column("tagId" , sql.Integer , primary_key=True),
... (not important)
)
TagToProductTable = sql.Table(
"TagToProduct", meta.metadata,
sql.Column("tagId" , sql.Integer ,
sql.ForeignKey("Tag.tagId"), nullable=False),
sql.Column("productId" , sql.Integer ,
sql.ForeignKey("Product.productId"), nullable=False)
)
My mappers configuration:
====================
orm.mapper(Product, Product._table,
properties={
"tags": orm.relation(Tag, secondary=TagToProductTable),
}
)
orm.mapper(Tag, Tag._table,
properties={}
)
====================
Currently I can filter products by ONE tag using join, the query looks
like this:
Session().query(Product).join(Product.tags, TagTable.c.tagId ==
tagId).filter(...).order_by(...)
If I want to filter by firstTag AND secondTag this of course not
works. Is here any easy solution for this problem?
Thanks for ideas
--
Best regards
- Petr Kobalicek <http://kobalicek.com>
--
You received this message because you are subscribed to the Google Groups
"sqlalchemy" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/sqlalchemy?hl=en.