mapper has_property method returns False when hybrid_property

Issue #3115 closed
ElGans created an issue

When I try to get property from mapper by name I can't do it when hybrid_property name specified.

import datetime
from sqlalchemy import create_engine, MetaData
from sqlalchemy import Column, String, DateTime, Integer
from sqlalchemy.ext.declarative import declarative_base, declared_attr
from sqlalchemy.ext.hybrid import hybrid_property
from sqlalchemy.orm import sessionmaker    

metadata = MetaData()
Base = declarative_base(metadata=metadata)    

class Duration(Base):
    __tablename__ = "duration"

    pk = Column(Integer, primary_key=True)
    name = Column(String, nullable=False)

    @hybrid_property
    def duration(obj):
        return obj.name    

engine = create_engine('sqlite:///:memory:', echo=True)
metadata.create_all(engine)
Session = sessionmaker(bind=engine)
session = Session()

duration = Duration(name="Test")
session.add(duration)
session.commit()    

print duration.__mapper__.has_property('duration') # Returns False, needs True

As you can see the has_property('duration') does not see the hybrid_property duration and returns False instead True.

Is it a bug?

Source: http://stackoverflow.com/questions/24579185/sqlalchemy-how-mapper-has-property-method-to-do-return-true-when-hybrid-propert

Comments (4)

  1. Mike Bayer repo owner

    while there's some poor naming quality going on here, a hybrid is not a MapperProperty, which is what the mapper considers to be "properties". To suit the use case where users want to view all class members that are at-all ORM specific, not just MapperProperty, we have all_orm_descriptors:

    print "duration" in duration.__mapper__.all_orm_descriptors
    

    http://docs.sqlalchemy.org/en/rel_0_9/orm/mapper_config.html?highlight=all_orm_descriptors#sqlalchemy.orm.mapper.Mapper.all_orm_descriptors

  2. Log in to comment