Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SimilarQuerySet specifies the field name in an ambiguous manner #16

Open
ishirav opened this issue Feb 2, 2015 · 1 comment
Open

SimilarQuerySet specifies the field name in an ambiguous manner #16

ishirav opened this issue Feb 2, 2015 · 1 comment

Comments

@ishirav
Copy link

ishirav commented Feb 2, 2015

Let's say you do something like:

Person.objects.filter_o(name__similar='Bobby').select_related('Employer')

if the Employer model also has a name field, you'll get an exception:

Exception Value: column reference "name" is ambiguous
LINE 1: SELECT (similarity(name, 'Bobby')) AS "name_distance...

The SimilarQuerySet class does not include the table name when specifying the field to filter on. To solve this problem you need something like this:

    def filter_o(self, **kwargs):
        qs = super(SimilarQuerySet, self).filter(**kwargs)
        for lookup, query in kwargs.items():
            if lookup.endswith('__similar'):
                field =  lookup.replace('__similar', '')
                select = {'%s_distance' % field: "similarity(%s.%s, '%s')" % (self.model._meta.db_table, field, query)}
                qs = qs.extra(select=select).order_by('-%s_distance' % field)
        return qs

(notice the addition of self.model._meta.db_table).

@jleivaizq
Copy link
Owner

Thanks. It's pretty easy to update.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants