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
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With