On Aug 3, 8:43 am, Alvaro Reinoso <alvrein...@gmail.com> wrote:
> Hello,
>
> I have these classes where items (class Item) is related to channel
> object. Channel can contain many items:
>
> channel_items = Table(
>         "channel_items",
>         metadata,
>         Column("channel_id", Integer,
>             ForeignKey("channels.id")),
>         Column("item_id", Integer,
>             ForeignKey(Item.id))
>     )
>
> class Channel(rdb.Model):
>     rdb.metadata(metadata)
>     rdb.tablename("channels")
>
>     id = Column("id", Integer, primary_key=True)
>     title = Column("title", String(100))
>
>     items = relation(Item, secondary=channel_items,
> backref="channels")
>
> class Item(rdb.Model):
>     rdb.metadata(metadata)
>     rdb.tablename("items")
>
>     id = Column("id", Integer, primary_key=True)
>     title = Column("title", String(100))
>
> I know how to get all the columns using something like:
>
> session = rdb.Session() channels =
> session.query(Channel).order_by(Channel.title)
>
> However, I'd like to select some columns from both tables with some
> conditions in Item. For example, select all the channels where
> item.type = 'jpg'. I'd like to get a channel object with items
> attributes with that condition for example. How can I do that?
>
> I've tried something like (no one worked out):
>
> result = session.query(Channel).filter(Item.typeItem != 'zeppelin/
> channel').all()
> result = session.query(Channel, Item).filter(Item.typeItem !=
> 'zeppelin/channel').all()
>
> Thanks in advance!

Try something like

session.query(Channel).join('items').filter(Item.typeItem !=
'whatever').all()

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalch...@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.

Reply via email to