Re: [sqlalchemy] To select a few columns in some tables related

2010-08-09 Thread Michael Bayer

On Aug 9, 2010, at 6:26 PM, Alvaro Reinoso wrote:

> Hello,
> 
> I have these classes:
> 
>class Channel(rdb.Model):
>   rdb.metadata(metadata)
>   rdb.tablename("channels")
> 
>   id = Column("id", Integer, primary_key=True)
>   title = Column("title", String(100))
> 
> 
>   items = relationship("MediaItem", secondary=channel_items,
> order_by="MediaItem.titleView", backref="channels")
> 
>class MediaItem(rdb.Model):
>   rdb.metadata(metadata)
>   rdb.tablename("media_items")
> 
>   id = Column("id", Integer, primary_key=True)
>   title = Column("title", String(100))
> 
>class User(rdb.Model):
>   rdb.metadata(metadata)
>   rdb.tablename("users")
> 
>   id = Column("id", Integer, primary_key=True)
>   name = Column("name", String(50))
> 
>   channels = relationship("Channel", secondary=user_channels,
> order_by="Channel.titleView", backref="users")
> 
> MediaItem is related to Channel and Channel is related to User.
> 
> if I'd like to select some columns from items and channels, I'd do
> this:
> 
>session = Session()
>result =
> session.query(Channel).join(Channel.items).values(Channel.title,
> Item.title)
> 
> I get an instance of Channel class with its items.
> 
> My problem is I don't know how to select some columns from User,
> Channel and Item. How can I make a query where for example, I can
> select the User.name property and its channels with only Channel.title
> property and the items of those channels with only Item.title
> property?

the form illustrated in the second example of 
http://www.sqlalchemy.org/docs/ormtutorial.html#querying selects individual 
columns, as does the "values()" method you're already using (i.e. 
values(User.name, Channel.title, Item.title) ).   You'd also want to add an 
extra join for "Channel.users".

-- 
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.



[sqlalchemy] To select a few columns in some tables related

2010-08-09 Thread Alvaro Reinoso
Hello,

I have these classes:

class Channel(rdb.Model):
rdb.metadata(metadata)
rdb.tablename("channels")

id = Column("id", Integer, primary_key=True)
title = Column("title", String(100))


items = relationship("MediaItem", secondary=channel_items,
order_by="MediaItem.titleView", backref="channels")

class MediaItem(rdb.Model):
rdb.metadata(metadata)
rdb.tablename("media_items")

id = Column("id", Integer, primary_key=True)
title = Column("title", String(100))

class User(rdb.Model):
rdb.metadata(metadata)
rdb.tablename("users")

id = Column("id", Integer, primary_key=True)
name = Column("name", String(50))

channels = relationship("Channel", secondary=user_channels,
order_by="Channel.titleView", backref="users")

MediaItem is related to Channel and Channel is related to User.

if I'd like to select some columns from items and channels, I'd do
this:

session = Session()
result =
session.query(Channel).join(Channel.items).values(Channel.title,
Item.title)

I get an instance of Channel class with its items.

My problem is I don't know how to select some columns from User,
Channel and Item. How can I make a query where for example, I can
select the User.name property and its channels with only Channel.title
property and the items of those channels with only Item.title
property?

Thanks in advance!

-- 
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.