synonyms don't handle PropComparator; make more powerful synonym() function

Issue #801 resolved
Mike Bayer repo owner created an issue

the use case for synonym should be set up with something less verbose and which actually supports instrumentation: i.e.

class Foo(object):
    def _get_bar(self):
        return self._bar
    def _set_bar(self, value):
        self._bar = value

mapper(Foo, bar, properties={
    'someprop' : synonym('_bar', column=bar.c.somecol, getter=Foo._get_bar, setter=Foo._set_bar)
})

use two InstrumentedAttributes here, and have the 'someprop' InstrumentedAttribute replace its getter/setter with the custom ones sent in. have mapper._compile_property() pull out a separate ColumnProperty from the SynonymProperty (i.e. we use two MapperProperties here too).

Comments (3)

  1. jek

    rather than getter=, etc., how about simply wrapping a user's 'property' instance with instrumentation. a synonym wrapper can reuse the impl guts from the base property.

        class AliasedAttribute(InstrumentedAttribute):
            def __init__(self, user_prop, orm_impl, comparator=None):
                self.user_prop = user_prop
                InstrumentedAttribute.__init__(self, orm_impl,
                                               comparator=comparator)
            def __get__(self, obj, owner):
                if obj is None:
                    self.user_prop.__get__(obj, owner)                
                    return self
                return self.user_prop.__get__(obj, owner)
            def __set__(self, obj, key, value):
                return self.user_prop.__set__(obj, key, value)
            def __delete__(self, obj, key):
                return self.user_prop.__delete__(obj, key)
    
  2. Log in to comment