Yannick Gingras | 13 May 22:50
Favicon

Query filtering like isinstance()

Greetings Alchemists, 
  some part of my application receives a partly filtered query and
applies additional criteria.  At one place, the query is on an object
hierarchy and I want to do a filter similar to isinstance().  Let's
look at this example:

------------------------------
items_table = Table('items', meta,
                    Column('id', Integer, primary_key=True),
                    Column('name', UnicodeText, nullable=False, 
                           unique=True),
                    Column('_type', Text, nullable=False))
class Item(object):
    def __repr__(self):
        return "<%s %s>" % (self.__class__.__name__, self.name)

mapper(Item, items_table, 
       polymorphic_on=items_table.c._type, 
       polymorphic_identity='item')

containers_table = Table('containers', meta,
                         Column('id', Integer, 
                                ForeignKey("items.id"),
                                primary_key=True))
class Container(Item):
    pass

mapper(Container, containers_table, 
       inherits=Item, polymorphic_identity='container')

(Continue reading)

Michael Bayer | 13 May 23:03

Re: Query filtering like isinstance()


On May 13, 2008, at 4:53 PM, Yannick Gingras wrote:

>
> If I want to query on containers and on area, I can simply do
>
>  q = Container.query().filter(...)
>
> but, if I receive a query on Item and a base class, say either Item,
> Container or Area, how can I filter() my query to receive only the
> sub-items from this base class?

filter on type_.in(["area", "container"]) is one approach.  Easier  
though is session.query(Container); it'll load from the join of items/ 
containers so you wouldn't get any non-Container objects.   
with_polymorphic() only speaks to what subclasses are eagerly loaded  
and will be available for filtering.   Though its something to think  
about if base classes are not included in WP, should that change the  
base class;  I'd say no, since you are reversing intent.

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

Yannick Gingras | 14 May 16:56
Favicon

Re: Query filtering like isinstance()


Michael Bayer <mike_mp <at> zzzcomputing.com> writes:

>> If I want to query on containers and on area, I can simply do
>>
>>  q = Container.query().filter(...)
>>
>> but, if I receive a query on Item and a base class, say either Item,
>> Container or Area, how can I filter() my query to receive only the
>> sub-items from this base class?
>
> filter on type_.in(["area", "container"]) is one approach.  Easier  
> though is session.query(Container); it'll load from the join of items/ 
> containers so you wouldn't get any non-Container objects.   

Sounds good.  I didn't find how to get the polymorphic_identity of a
mapped class.  Is it possible to retried it if I have only the class
object?  This is not a big problem since I can use Item.__name__ as
the polymorphic_identity.

--

-- 
Yannick Gingras

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

(Continue reading)

Michael Bayer | 14 May 17:04

Re: Query filtering like isinstance()


On May 14, 2008, at 10:56 AM, Yannick Gingras wrote:

>
> Michael Bayer <mike_mp <at> zzzcomputing.com> writes:
>
>>> If I want to query on containers and on area, I can simply do
>>>
>>> q = Container.query().filter(...)
>>>
>>> but, if I receive a query on Item and a base class, say either Item,
>>> Container or Area, how can I filter() my query to receive only the
>>> sub-items from this base class?
>>
>> filter on type_.in(["area", "container"]) is one approach.  Easier
>> though is session.query(Container); it'll load from the join of  
>> items/
>> containers so you wouldn't get any non-Container objects.
>
> Sounds good.  I didn't find how to get the polymorphic_identity of a
> mapped class.  Is it possible to retried it if I have only the class
> object?  This is not a big problem since I can use Item.__name__ as
> the polymorphic_identity.

class_mapper(cls).polymorphic_identity should work

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "sqlalchemy" group.
To post to this group, send email to sqlalchemy <at> googlegroups.com
To unsubscribe from this group, send email to sqlalchemy-unsubscribe <at> googlegroups.com
(Continue reading)

Yannick Gingras | 14 May 17:09
Favicon

Re: Query filtering like isinstance()


Michael Bayer <mike_mp <at> zzzcomputing.com> writes:

>> Sounds good.  I didn't find how to get the polymorphic_identity of a
>> mapped class.  Is it possible to retried it if I have only the class
>> object?  This is not a big problem since I can use Item.__name__ as
>> the polymorphic_identity.
>
> class_mapper(cls).polymorphic_identity should work

It does.

Thanks!

--

-- 
Yannick Gingras

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


Gmane