Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Association_proxy with 0-n relations

I got an error trying to use an association_proxy.

I got mapped class A, with 0-n relation to B. B has 0-n relation to C. The association_proxy is to access A from C.

class C(base):
    a = association_proxy('b', 'a')

It works without problem if it really has a relation to B. But if this relation is null, then trying to access myCinstance.a throws a: AttributeError 'NoneType' object has no attribute 'a'. I guess it works well with 1-n relation, but is there a way that myCinstance.a returns None instead of an error? (I saw the creator option, but looks to be only for setting, not getting).

Thanks in advance.

I’m using SqlAlchemy 0.7.5

EDIT: I came up with a simple example describing the problem https://gist.github.com/2225046

like image 535
tonio Avatar asked Feb 22 '23 00:02

tonio


1 Answers

I believe from reading http://docs.sqlalchemy.org/en/latest/orm/extensions/associationproxy.html#querying-with-association-proxies that the case is handled when you are issuing a query (ie the SQL uses an EXISTS clause to catch the non existing B issue).

In order to get the accessor shortcut to work, you need to use the getset_factory argument:

def outer_join_accessor_factory(collection_type, proxy):
    def getter(obj):
        if obj is None:
            return None
        return getattr(obj, proxy.value_attr)
    def setter(obj, value):
        setattr(obj, proxy.value_attr, value)
    return getter, setter

class C(Base):
    __tablename__ = 'c'
    id = Column(Integer, primary_key=True)
    id_b = Column(Integer, ForeignKey('b.id'))
    b = relationship('B', backref='cs')
    a = association_proxy('b', 'a', getset_factory=outer_join_accessor_factory)
like image 199
gurney alex Avatar answered Feb 23 '23 14:02

gurney alex