I have this general structure:

    class Person:
         # orm relationships are preceded by (o)ne or (l)ist  
         o_Person2Address_ActiveShipping = sa.orm.relationship( "
Person2Address", primaryjoin="""and_( Person2Address.person_id==Person.id , 
Person2Address.role_id=='active-shipping' )""", uselist=False )         
        active_shipping_address = association_proxy('
o_Person2Address_ActiveShipping', 'address')
    
    class Person2Address:
        address = sa.orm.relationship("Address", 
primaryjoin="Person2Address.address_id==Address.id")

    class Address:
       pass

this works perfect when i have a Person2Address and address .  I'd imagine 
it works fine if the proxy is for an empty list too.

the problem is when o_Person2Address_ActiveShipping is an empty scalar 
(from the uselist=False argument).

   jim = dbSession.query( Person )
   active_shipping = jim.o_Person2Address_ActiveShipping 
   type(active_shipping)
   >> None

   # this will raise an error
   if jim.active_shipping_address :

      # this will raise an error too
       if jim.active_shipping_address 
and jim.active_shipping_address.address :

          print jim.active_shipping_address

that raises an error on the .active_shipping_address

File 
"/Users/jvanasco/webserver/environments/project-2.7.5/lib/python2.7/site-packages/sqlalchemy/ext/associationproxy.py",
 line 241, in __get__
    return self._scalar_get(getattr(obj, self.target_collection))
AttributeError: 'NoneType' object has no attribute 'media_asset'



i think a simple fix could be something like this ( line 240, 
sqlalchemy/ext/associationproxy.py 
)

        if self.scalar:
-            if not getattr(obj, self.target_collection)
-            return self._scalar_get(getattr(obj, self.target_collection))
        else:

        if self.scalar:
+            proxied = getattr(obj, self.target_collection)
+            if not proxied :
+                return None
+            return self._scalar_get(proxied)
      else:







-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to