On 08/11/2016 04:15 AM, Dominik George wrote:
Hi,

first of all, thanks for SQLAlchemy - it's the one project that actually
made me enjoy working with large data collections in applications ;)!

I have an SQLAlchemy model that associates tags (key/value pairs) with
elements, like in attached mwe.py.


Element.tags is a dictionary collection, with some proxying to make it
appear just like a plain dict, which works really well:


|session =sessionmaker(bind=engine)()element =Element()element.tags
={u"foo":u"bar",u"bang":u"baz"}session.add(element)session.commit()

|Now, how would I go about getting all |Element|s that have a certain key/value 
pair attached as tag, using |session.query(Element)|?

I found this documentation
<http://docs.sqlalchemy.org/en/latest/orm/extensions/associationproxy.html#querying-with-association-proxies>
 at 
http://docs.sqlalchemy.org/en/latest/orm/extensions/associationproxy.html#querying-with-association-proxies
 from which I'd derive |session.query(Element).filter(Element.tags.has(key="foo",
value="bar")).all()|. However, this throws:

|Traceback(most recent call last):File"<stdin>",line
1,in<module>File"/usr/lib/python2.7/dist-packages/sqlalchemy/ext/associationproxy.py",line
409,inhas
ifself._target_is_object:File"/usr/lib/python2.7/dist-packages/sqlalchemy/util/langhelpers.py",line
754,in__get__ obj.__dict__[self.__name__]=result
=self.fget(obj)File"/usr/lib/python2.7/dist-packages/sqlalchemy/ext/associationproxy.py",line
244,in_target_is_object
returngetattr(self.target_class,self.value_attr).impl.uses_objects
AttributeError:'AssociationProxy'object has no attribute 'impl'


Unfortunately this is not supported right now and I've created https://bitbucket.org/zzzeek/sqlalchemy/issues/3769/chained-any-has-with-association-proxy which includes a patch so that this can be provided in the future.




I am using SQLAlchemy 1.0.14 from Debian, in this example with SQLite.

Cheers,
Nik
|

--
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 sqlalchemy+unsubscr...@googlegroups.com
<mailto:sqlalchemy+unsubscr...@googlegroups.com>.
To post to this group, send email to sqlalchemy@googlegroups.com
<mailto:sqlalchemy@googlegroups.com>.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

--
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 sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to