its impossible to know what you want without seeing literal SQL but this
is the general idea

from sqlalchemy import *
from sqlalchemy.orm import *
from sqlalchemy.ext.declarative import declarative_base

Base = declarative_base()

class USCity(Base):
    __tablename__ = 'cities'
    id = Column(Integer, primary_key=True)
    name = Column(String(50))

    @property
    def zipcode_meta(self):
        return object_session(self).query(USZipCode.city_id,
USZipCode.population,
USZipCode.avg_house_value).filter(USZipCode.city_id==self.city_id)

class USZipCode(Base):
    __tablename__ = 'zipcodes'
    id = Column(Integer, primary_key=True)
    city_id = Column(Integer, ForeignKey('cities.id'))
    zip = Column(String(12))
    population = Column('population', Integer)
    avg_house_value = Column('avg_house_value', Integer)




indigophone wrote:
>
> I gave it a shot but I am no closer to knowing how to do this.
>
> On Nov 19, 12:00 am, Michael Bayer <[EMAIL PROTECTED]> wrote:
>> joining to a subquery is better accomplished outside of relation()  
>> using query, such as query(USZipCode).join((subquery,  
>> subquery.c.col==USZipCode.somecol)).
>>
>> Now you want it as an attribute on your class.  Do it like this:
>>
>> class USCity(object):
>>      ...
>>
>>     [EMAIL PROTECTED]
>>      def zipcode_meta(self):
>>          return object_session(self).query(USZipCode).join(...join  
>> criterion...).params(..whatever...)
>>
>> The advantage to this is that you can formulate the query and its  
>> relation to the parent in exactly the way you need.
>>
>> On Nov 18, 2008, at 9:34 PM, indigophone wrote:
>>
>>
>>
>> > zipcode_meta_join_subquery = session.query(us_zipcode_table.c.city_id,
>> > us_zipcode_table.c.zipcode_population,
>> > us_zipcode_table.c.average_house_value).group_by(us_zipc
>> > ode_table.c.city_id).subquery()
>>
>> > mapper(USCity, us_city_table, properties={
>> >    'state':relation(USState, backref=backref('cities')),
>> >    'zipcode_meta':relation(USZipCode, primaryjoin=
>> > (zipcode_meta_join_subquery,
>> > zipcode_meta_join_subquery.c.city_id==us_city_table.c.city_id))
>> > })
>>
>> > The above code obviously doesn't work. How do I add a join to the
>> > above subquery in my mapper?
> >
>


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