Skip to content

supply a pre-built comparator on associationproxy instead of requiring homegrown #1372

Closed
@sqlalchemy-bot

Description

@sqlalchemy-bot
Collaborator

Migrated issue, originally created by Anonymous

Let's consider the example given in the documentation.
One can do:

s.query(User).filter(User.user_keywords.contains(user_keyword))

but he can't do:

s.query(User).filter(User.keywords.contains(keyword))

because:

AttributeError: 'AssociationProxy' object has no attribute 'contains'

That's why, when I use the association_proxy pattern, I also add another view only relation between the two referenced objects (in this case User and Keyword), using the secondary table.
I use this relation for selecting, and I use the association_proxy relation for inserting...

This duplication is annoying. I don't know if it's possible to solve the problem, but I want to be able to do:

s.query(User).filter(User.keywords.contains(keyword))

kobipe3@gmail.com


Attachments: ticket-1372-associationproxy-comparators.patch

Activity

sqlalchemy-bot

sqlalchemy-bot commented on Apr 6, 2009

@sqlalchemy-bot
CollaboratorAuthor

Anonymous wrote:

submitted by kobipe3@gmail.com

sqlalchemy-bot

sqlalchemy-bot commented on Apr 6, 2009

@sqlalchemy-bot
CollaboratorAuthor

Michael Bayer (@zzzeek) wrote:

it is possible using a comparable_property mapping to your descriptor:

http://www.sqlalchemy.org/docs/05/reference/orm/mapping.html#sqlalchemy.orm.comparable_property

build a Comparator and implement the comparison methods you'd like and they will be assembled on your associationproxy.

im wondering how hard it would be to build this into associationproxy directly.

sqlalchemy-bot

sqlalchemy-bot commented on Apr 6, 2009

@sqlalchemy-bot
CollaboratorAuthor

Changes by Michael Bayer (@zzzeek):

  • removed labels: bug
  • added labels: feature, orm
  • set milestone to "0.6.xx"
sqlalchemy-bot

sqlalchemy-bot commented on Apr 6, 2009

@sqlalchemy-bot
CollaboratorAuthor

Changes by Michael Bayer (@zzzeek):

  • changed title from "Make it possible to query on association_proxy" to "supply a pre-built comparator on associationproxy "
sqlalchemy-bot

sqlalchemy-bot commented on Oct 17, 2009

@sqlalchemy-bot
CollaboratorAuthor

Anonymous wrote:

I wanted to use the has and any functions on associationproxies, and didn't know about about comparable_property, so I added this to AssociationProxy:

import sqlalchemy.exceptions as sa_exc

#snip

    def any(self, criterion=None, **kwargs):
        if self._target_is_scalar():
            raise sa_exc.InvalidRequestError("'any()' not implemented for scalar attributes. Use has().")
        return self._get_property().comparator.any(getattr(self.target_class, self.value_attr).has(criterion, **kwargs))
    
    def has(self, criterion=None, **kwargs):
        if not self._target_is_scalar():
            raise sa_exc.InvalidRequestError("'has()' not implemented for collections.  Use any().")
        return self._get_property().comparator.has(getattr(self.target_class, self.value_attr).has(criterion, **kwargs))

I'll try using a comparable_property instead now, but if it's possible to implement a general solution for AssociationProxy, I'd be willing to try to figure out the other Comparator functions.

sqlalchemy-bot

sqlalchemy-bot commented on Nov 18, 2009

@sqlalchemy-bot
CollaboratorAuthor

Anonymous wrote:

Here's a patch against 26edef4 that adds proxying of .any(), .has(), .contains(), ==, and != to association_proxies. Lots of tests are included.

Any thoughts on whether or not this implementation is appropriate?

Also, please add storborg@mit.edu to the cc list on this ticket. Thanks!

sqlalchemy-bot

sqlalchemy-bot commented on Nov 18, 2009

@sqlalchemy-bot
CollaboratorAuthor

Anonymous wrote:

(original author: ged)

sqlalchemy-bot

sqlalchemy-bot commented on Jan 18, 2010

@sqlalchemy-bot
CollaboratorAuthor

Michael Bayer (@zzzeek) wrote:

hey this is a great patch. We just have to work the unittest to use MappedTest, Comparable and eq_ (at the very least speeds up the test to only setup/teardown once for the whole suite) and its good to go.

sqlalchemy-bot

sqlalchemy-bot commented on Jan 18, 2010

@sqlalchemy-bot
CollaboratorAuthor

Changes by Michael Bayer (@zzzeek):

  • changed milestone from "0.6.xx" to "0.6.0"
sqlalchemy-bot

sqlalchemy-bot commented on Jan 22, 2010

@sqlalchemy-bot
CollaboratorAuthor

Michael Bayer (@zzzeek) wrote:

Scott, you are the man. this is in 2d15d9b thanks !

sqlalchemy-bot

sqlalchemy-bot commented on Jan 22, 2010

@sqlalchemy-bot
CollaboratorAuthor

Changes by Michael Bayer (@zzzeek):

  • changed status to closed
added this to the 0.6.0 milestone on Nov 27, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @sqlalchemy-bot

        Issue actions

          supply a pre-built comparator on associationproxy instead of requiring homegrown · Issue #1372 · sqlalchemy/sqlalchemy