#25427: Trying to do a full text search query on a database field (non model
field), the .annotate(val=RawSQL(...)).filter(val=VAL1) doesn't work.
-------------------------------------+-------------------------------------
     Reporter:  vladiibine           |                    Owner:  nobody
         Type:  Bug                  |                   Status:  new
    Component:  Database layer       |                  Version:  master
  (models, ORM)                      |
     Severity:  Normal               |               Resolution:
     Keywords:  QuerySet.extra       |             Triage Stage:  Accepted
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------

Comment (by rpkilby):

 We have a very similar usecase, and are running into the same issue when
 annotating with a RawSQL query. Noticed something very strange about the
 generated SQL.

 Using annotate + extra, you get the expected query
 {{{
 >>> print models.User.objects.only('id') \
 >>>     .annotate(val=RawSQL('select 3', params=[])) \
 >>>     .extra(where=['val=%s'], params=[3]) \
 >>>     .query

 SELECT "accounts_user"."id", (select 3) AS "val" FROM "accounts_user"
 WHERE (val=3)
 }}}

 If you try to use filter, and inspect with `sql_with_params`, you'll
 notice that `val` is substituted with the contents of the RawSQL query.
 {{{
 >>> q = models.User.objects.only('id') \
 >>>     .annotate(val=RawSQL('select 3', params=[])) \
 >>>     .filter(val=3).query
 >>> print q.sql_with_params()[0]
 SELECT "accounts_user"."id", (select 3) AS "val" FROM "accounts_user"
 WHERE (select 3) = %s
 }}}

 Also, this happens:
 {{{
 >>> print q.sql_with_params()[1]
 (3, 3)

 >>> print q.sql_with_params()[1]
 (3, 3, 3, 3)

 >>> print q.sql_with_params()[1]
 (3, 3, 3, 3, 3, 3)

 >>> print q.sql_with_params()[1]
 (3, 3, 3, 3, 3, 3, 3, 3)
 }}}

--
Ticket URL: <https://code.djangoproject.com/ticket/25427#comment:2>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/068.23ee8983abab563d5877ca14b56f4ec6%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to