funny joins

Issue #198 resolved
Former user created an issue

(original reporter: Justin) I have 2 tables:

 hosts : ip address (inet type)
 subnets: netmask   (cidr type)

I want to join the 2 tables, but the query needs to be something like

select * from hosts h, left join subnets s on h.ip << s.subnet;

right now I accomplish what I want to do with properties in the class:

class Subnet...:
    def _get_hosts(self):
        return Host.select(Host.c.ip._compare("<<", self.subnet))
    hosts = property(_get_hosts)

It only needs to be read only, as you don't do

h = Host.get(1)
s  = Subnet.get(2)
h.subnet = s

Instead, the .host and .subnet are pulled in based on what the address is.

Comments (4)

  1. Former user Account Deleted

    (original author: anonymous) I do not understand the join condition "on h.ip << s.subnet". Could you give an example? For me, "a << b" shifts a from b bits to the left. Do you assume that the result is constrained to 32 bits?

  2. Mike Bayer repo owner

    sorry, have to put this on the back burner for awhile, unless someone wants to play with it. the recent refactorings in the property system should help.

  3. Former user Account Deleted

    (original author: ants) This has been possible for some time now.

    mapper(Subnet, subnets, properties={
       'hosts': relation(Host,
           primaryjoin=hosts.c.ip.op('<<')(subnets.c.netmask),
           viewonly=True, foreign_keys=[hosts.c.ip](hosts.c.ip), uselist=True)})
    
  4. Log in to comment