...and we're back to the same issue with another column_property: that is, How do you create column_properties with subqueries on child objects where the subquery references another child object? This:
PC_avgdist_subq = select([func.avg(func.distance(PC.pt.RAW, Switch.pt.RAW))]).where(PC.switch_mac == Switch.mac).as_scalar() Switch.PC_avgdist = column_property(PC_avgdist_subq) no longer works. The workaround worked for the last case but doesn't work when I need to do a func.average(func.distance(..)) to get the average physical distance between a switch and the PCs that are attached to it. As before, the generated sql is appending "WHERE 'devtype' in 'Switch'" to the end of the switch = Session.query(Switch)... statement, which I think is throwing off the subquery results (but I could be completely wrong). Thanks, S. On Aug 4, 5:11 pm, SQLAlchemy User <[email protected]> wrote: > ...and I've figured out a workaround: > > in Switch(Device): > pcs = relationship('PC', primaryjoin = 'Switch.mac == > PC.switch_mac', lazy='dynamic') > > Now, switch.pcs.count() works :) > > S. > > On Aug 4, 5:00 pm, Zippy P <[email protected]> wrote: > > > > > I have a single-table inheritance setup: > > > class Device(Base): > > __tablename__ = 'devices' > > devtype = Column(Unicode(20), nullable = False) > > mac = Column(Unicode(128), primary_key = True) > > ... > > > class PC(Device): > > __mapper_args_ = {'polymorphic_identity':u'PC'} > > switch_mac = Column(Unicode(128), ForeignKey(mac), nullable = True) # > > ignore any typos here - this is munged code > > > class Switch(Device): > > __mapper_args_ = {'polymorphic_identity':u'Switch'} > > ... > > > pc_ct_subq = select([func.count(PC.mac)]).where(PC.switch_mac == > > Switch.mac).as_scalar() > > Switch.pc_ct = column_property(pc_ct_subq) > > > When I didn't have the inheritance set up (when Switch and PC were separate > > tables and separate objects), pc_ct worked fine. Now, however, it's failing > > - I think because the inheritance is appending "AND devices.devtype IN 'AP'" > > to the initial load query since it's trying to bring up just devices of type > > 'Switch'. > > > How do I get around this? I just want Switch to have an attribute that > > represents the number of PCs that are associated to it (where the PC's > > switch_mac equals the Switch's mac). > > > Thanks, > > > S. -- You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To post to this group, send email to [email protected]. 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.
