functional discriminator; row awareness, etc.

Issue #929 resolved
Mike Bayer repo owner created an issue

to make it easier to support joined-table inheritance with no explcit discriminator, as well as any functional situation:

from sqlalchemy.orm import joined_discriminator
mapper(Cls, table, 
select_table=table.outerjoin(subtable1).outerjoin(subtable2), polymorphic_on=joined_discriminator, ...)

polymorphic_on accepts a callable. at result time, it gets called:

if callable(self.polymorphic_on):
    discriminator = self.polymorphic_on(self, row)
    ...etc ...

we can skip the "callable" check by just making a string-based polymorphic_on into a callable of its own.

joined_discriminator then works like this (will have to optimize this):

def joined_discrminator(mapper, row):
    for key, submapper in mapper.polymorphic_map.iteritems():
        if row[submapper.local_table.primary_keys[0](submapper.local_table.primary_keys[0)]:
            return key
    return mapper.polymorphic_identity

i.e. it looks at all the joined tables and returns the discriminator for the one thats present in the row. primary_keys0 might not be possible depending on how the outerjoin is set up (also the base table pk wont get confused for the sub-table pk ?)

Comments (4)

  1. Log in to comment