I see... I'll work something out.

Thank you Mr. Bayer!!

2011/3/1 Michael Bayer <mike...@zzzcomputing.com>:
>
> On Mar 1, 2011, at 5:50 PM, Hector Blanco wrote:
>
> Hello everyone:
>
> Let's say I have a class "User"  and a class "UserGroup". One user can
> belong to one userGroup, an a userGroup can contain several users
> (pretty typical structure). It's a simple relationship I got modeled
> like:
>
> class UserGroup(declarativeBase):
>       """Represents a group of users with the same features"""
>       __tablename__ = "user_groups"
>
>       id = Column("id", Integer, primary_key=True)
>       name = Column("name", String(50))
>       users = relationship("User", order_by=lambda:User.userName,
> cascade="all, delete", collection_class=set)
>
> class User(declarativeBase):
>       """Represents a user"""
>       __tablename__ = "users"
>
>       id = Column("id", Integer, primary_key=True)
>       firstName = Column("first_name", String(50))
>       lastName = Column("last_name", String(50))
>       email = Column("email", String(60))
>       userName = Column("user_name", String(50), unique=True,
> nullable=False)
>       password = Column("password", String(64), nullable=False)
>       userGroupId = Column("user_group_id", Integer,
> ForeignKey("user_groups.id"))
>
>       userGroup = relationship("UserGroup", uselist=False)
>
> I am working in a tool that accepts generic queries, and, basically, I
> can do something like:
>
> session.query(User.User).filter(User.User.id > 3).values("userName")
>
> And get tuples with a .userName field with all the userNames of the
> users whose id is > 3
>
> But if I try:
> session.query(User.User).filter(User.User.id > 3).values("userGroup")
>
> well yes values() accepts only scalar columns (and also you should pass the
> attribute, not a string, guess the docs aren't crystal clear on that).
>
>
> So here's the question:
>
> Is there any way of getting the "userGroup" value somehow "starting"
> (or querying) User objects? (or what would be the best way, if there
> are many ways)
>
> typically the columns you're retrieving are the thing you're "starting"
> from:
>
> query(UserGroup).join(UserGroup.users).filter(User.id > 3).all()
> if you have a lot more join going on and really need a certain entity in the
> left, you can say:
> query(UserGroup).select_from(User).join(User.userGroup).filter(User.id >
> 3).all()
> There's a ticket somewhere to allow query() to also accept a relationship()
> attribute that is specifically many-to-one, but that's just a small
> syntactic convenience.   query() in general accepts entities and column
> expressions only.
>
> --
> You received this message because you are subscribed to the Google Groups
> "sqlalchemy" group.
> To post to this group, send email to sqlalchemy@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.
>

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalchemy@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