Re: [Sqlalchemy-tickets] [sqlalchemy] #2872: AliasedClass.__getattr__() creates malformed Queryable
Brought to you by:
zzzeek
|
From: sqlalchemy <mi...@zz...> - 2013-11-23 05:07:12
|
#2872: AliasedClass.__getattr__() creates malformed QueryableAttribute
---------------------------+-------------------------------
Reporter: elic | Owner: zzzeek
Type: defect | Status: new
Priority: high | Milestone: 0.9.0
Component: orm | Severity: major - 1-3 hours
Resolution: | Keywords:
Progress State: in queue |
---------------------------+-------------------------------
Changes (by zzzeek):
* priority: medium => high
* milestone: 0.9.xx => 0.9.0
* severity: no triage selected yet => major - 1-3 hours
* status_field: awaiting triage => in queue
Comment:
unusually, the verbal description of the case here gives us a test case
simply, whereas the test case posted here is, mmm, very scary! A
descriptor might return a `QueryableAttribute` and we need to adapt that,
so that suggests a simple @hybrid_proprerty needs this fix, and i think
you are right. Here's a patch on master:
{{{
#!diff
index 9737072..74ea592 100644
--- a/lib/sqlalchemy/orm/util.py
+++ b/lib/sqlalchemy/orm/util.py
@@ -341,9 +341,12 @@ class AliasedClass(object):
return None
elif hasattr(attr, '__get__'):
ret = attr.__get__(None, self)
- if isinstance(ret, PropComparator):
+ if isinstance(ret, attributes.QueryableAttribute):
+ return _aliased_insp._adapt_prop(ret, key)
+ elif isinstance(ret, PropComparator):
return ret.adapt_to_entity(_aliased_insp)
- return ret
+ else:
+ return ret
else:
return attr
}}}
simple test seems to show it:
{{{
#!python
from sqlalchemy import *
from sqlalchemy.orm import *
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.ext.hybrid import hybrid_property
Base = declarative_base()
class A(Base):
__tablename__ = 'a'
id = Column(Integer, primary_key=True)
data = Column(String(50))
@hybrid_property
def foo_data(self):
return self.data
print A.foo_data
a1 = aliased(A)
print a1.data
print a1.foo_data
}}}
otherwise the `QueryableAttribute._adapt_to_entity()` method that gets
called otherwise here is inadequate. At the moment it seems like that
system should be clarified - why *cant*
`QueryableAttribute._adapt_to_entity()` do the right thing? I might try
to figure out why that is. But yes, the patch you have already is the
immediate fix. Not really sure about that metaclass test though, just a
simple descriptor is all we need to illustrate?
--
Ticket URL: <http://www.sqlalchemy.org/trac/ticket/2872#comment:1>
sqlalchemy <http://www.sqlalchemy.org/>
The Database Toolkit for Python
|