[Sqlalchemy-tickets] Issue #4351: like() in association_proxy (zzzeek/sqlalchemy)
Brought to you by:
zzzeek
From: Abdeali K. <iss...@bi...> - 2018-10-23 03:50:19
|
New issue 4351: like() in association_proxy https://bitbucket.org/zzzeek/sqlalchemy/issues/4351/like-in-association_proxy Abdeali Kothari: This is an issue I'm creating for the situation described in: https://stackoverflow.com/questions/36017947/filter-with-like-on-sqlalchemy-association-proxy I am creating the issue here to understand if there is an interest in adding this functionality to the core library. If there is interest in adding like() filters to association proxies i would be interested in contributing and making a pull-request for it The example here is: ``` class Institution(Base): entity_id = Column( String(8, u'SQL_Latin1_General_CP1_CI_AS'), ForeignKey(Entity.entity_id), primary_key=True) entity = relationship(Entity, uselist=False) name = association_proxy('entity', 'entity_proper_name') ``` and the outputs: ``` query = s.query(Institution).filter(Institution.name=='Correct Name') # works fine query = s.query(Institution).filter(Institution.name.like('%Correct N%') # AttributeError: 'AssociationProxy' object has no attribute 'like' ``` A possible workaround is: ``` from sqlalchemy.ext.associationproxy import AssociationProxy def like_filter(attr, *args, **kwargs): filter_attr = attr if isinstance(filter_attr, AssociationProxy): # If assoc_proxy get remote_attr as like() doesnt work filter_attr = filter_attr.remote_attr return filter_attr.like(*args, **kwargs) ``` |