On Aug 5, 2010, at 5:06 PM, Zippy P wrote:

> I have the following code:
> 
> from objects import *
> 
> class Device(Base):
>       __tablename__="testdev"
>       devtype = Column(Unicode(20), primary_key = True)
>       __mapper_args__ = {'polymorphic_on': devtype}
>       mac = Column(Unicode(128), primary_key = True)
>       switch_mac = Column(Unicode(128), ForeignKey(mac))
>       switch = relationship('Device', primaryjoin = 'Device.switch_mac == 
> Device.mac', backref=backref('connections'), remote_side = [mac])   
>       cablelength = Column(Float, nullable = True)
> 
> class PC(Device):
>       __mapper_args__ = {'polymorphic_identity': u'P'}
>       
> class Switch(Device):
>       __mapper_args__ = {'polymorphic_identity': u'S'}
>       
> Switch.PC_cablelength_avg = column_property (
>       select([func.avg(PC.cablelength)]).where(PC.switch_mac == 
> Switch.mac).as_scalar()
> )

> >>> s = Switch(mac='s1')
> >>> p = PC(mac='p1',switch_mac=s.mac, cablelength = 100)
> >>> Session.add(p) # adds a PC with a cable length of 100 to switch s 
> >>> Session.add(s)
> >>> Session.commit()
> >>> s = Session.query(Switch).first()
> >>> s.PC_cablelength_avg
> >>>
> 
> Expected value for s.PC_cablelength_avg is 100.
> 
> The select statement (from first()) looks like this:
> 
> SELECT testdev.devtype AS testdev_devtype, testdev.mac AS testdev_mac, 
> testdev.switch_mac AS testdev_switch_mac, testdev.cablelength AS 
> testdev_cablelength, (SELECT avg(testdev.cablelength) AS avg_1 
> FROM testdev 
> WHERE testdev.switch_mac = testdev.mac) AS anon_1 
> FROM testdev 
> WHERE testdev.devtype IN (%s) 
>  LIMIT 0, 1
> ('S',)
> 
> 
> The issue, I think, is that the "WHERE testdev.devtype IN 'S'" is causing the 
> results to not include objects of type PC, which is what's used in the 
> column_property. 

Ultimately you're looking for a self-referential correlation here.   In SQL, 
that always implies using aliases.   Your column_property needs to be against 
aliased(PC) and not PC itself.    





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

Reply via email to