On Oct 27, 2008, at 10:43 AM, Adam wrote:

>
> I'm having trouble using in_ to select a bunch of related objects -
> here is what I want:
>
> Quick overview of my model (shortened for simplicity):
> #Autoloaded table defs go here and aren't really important
>
> class Person(object):
>    pass
>
> class EMailAddress(object):
>    pass
>
> orm.mapper(Person, person_table, properties = {
>       'name': orm.column_property(person_table.c.first_name + " " +
> person_table.c.last_name)
> })
>
> orm.mapper(EMailAddress, email_table, properties = {
>       'person': orm.relation(Person, lazy=False,
>               backref=backref('email_addresses', lazy=False))
> })
>
>
>
> I want to be able to do something like this:
> people = [person1, person2, person3]
>
> addresses = Session.query(EMailAddress).filter(Person.in_(people))
>
> When I try it, I get an AttributeError: "type object 'Person' has no
> attribute 'in_'"
>
> I'm running 0.5.0rc2
>
> Is this possible?

The "in_()" operator is not currently implemented for many-to-one- 
relation, its a TODO.  When it is implemented, it would be  
EmailAddress.person.in_(people).

so currently two ways to do it:

Session.query(EmailAddress).filter(or_(*[EmailAddress.person==p for p  
in people]))

Session.query(EmailAddress).filter(EmailAddress.person_id.in_([p.id  
for p in people]))

note that or_() takes *args, in_() takes a list.  Believe it or not  
there's a rationale for this.


Also I'd recommend a plain python descriptor for "Person.name" since  
it would reduce SQL overhead a bit:

@property
def name(self):
     return self.first_name + " " + self.last_name





--~--~---------~--~----~------------~-------~--~----~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to