[Sqlalchemy-tickets] Issue #3557: Hybrid attributes are under wrong name in result objects (zzzeek/
Brought to you by:
zzzeek
|
From: Łukasz F. <iss...@bi...> - 2015-10-19 18:32:27
|
New issue 3557: Hybrid attributes are under wrong name in result objects https://bitbucket.org/zzzeek/sqlalchemy/issues/3557/hybrid-attributes-are-under-wrong-name-in Łukasz Fidosz: When querying hybrid property it uses underlying column name instead of hybrid property name as attribute name in result object. So code bellow (please note the commented line on the bottom): ``` #!python from sqlalchemy import Integer, Column from sqlalchemy import create_engine from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.ext.hybrid import hybrid_property from sqlalchemy.orm import sessionmaker, scoped_session from sqlalchemy.types import TypeDecorator, Unicode, UnicodeText Base = declarative_base() class SomeClass(Base): __tablename__ = 'some_table' id = Column(Integer(), primary_key=True) raw_foo = Column('foo', Integer(), nullable=False, default=0) @hybrid_property def foo(self): if False: # some conditional code return self.raw_foo * 10 return self.raw_foo @foo.expression def country(cls): return cls.raw_foo @foo.setter def country(self, value): self.raw_foo = value engine = create_engine('sqlite://', echo=True) Session = scoped_session(sessionmaker(bind=engine)) session = Session() Base.metadata.create_all(engine) session.commit() session.add_all([ SomeClass(id=1, foo=1), SomeClass(id=2, foo=10), SomeClass(id=3, foo=30), ]) session.commit() q = session.query(SomeClass.id, SomeClass.foo) item = q.first() # the commented line will work so apparently it uses # underlying column as a name: # print item.raw_foo print item.foo ``` Will crash like this: ``` #!python Traceback (most recent call last): File "/home/virhilo/sqlalchemy_hybrids_error.py", line 47, in <module> print item.foo AttributeError: 'result' object has no attribute 'foo' ``` |