doableware/djongo

Django contains operator not working (LIKE)

muriloventuroso opened this issue · 4 comments

Operations like __contains, __icontains, __startswith and __endswith are not working.

Python script

User.objects.filter(username__icontains="admin")

Traceback

Traceback (most recent call last):
  File "/home/muriloventuroso/Dev/Python/sgc-env/lib/python3.10/site-packages/djongo/sql2mongo/query.py", line 926, in parse
    return handler(self, statement)
  File "/home/muriloventuroso/Dev/Python/sgc-env/lib/python3.10/site-packages/djongo/sql2mongo/query.py", line 1002, in _select
    return SelectQuery(self.db, self.connection_properties, sm, self._params)
  File "/home/muriloventuroso/Dev/Python/sgc-env/lib/python3.10/site-packages/djongo/sql2mongo/query.py", line 121, in __init__
    super().__init__(*args)
  File "/home/muriloventuroso/Dev/Python/sgc-env/lib/python3.10/site-packages/djongo/sql2mongo/query.py", line 67, in __init__
    self.parse()
  File "/home/muriloventuroso/Dev/Python/sgc-env/lib/python3.10/site-packages/djongo/sql2mongo/query.py", line 157, in parse
    self.where = WhereConverter(self, statement)
  File "/home/muriloventuroso/Dev/Python/sgc-env/lib/python3.10/site-packages/djongo/sql2mongo/converters.py", line 28, in __init__
    self.parse()
  File "/home/muriloventuroso/Dev/Python/sgc-env/lib/python3.10/site-packages/djongo/sql2mongo/converters.py", line 120, in parse
    self.op = WhereOp(
  File "/home/muriloventuroso/Dev/Python/sgc-env/lib/python3.10/site-packages/djongo/sql2mongo/operators.py", line 501, in __init__
    self._statement2ops()
  File "/home/muriloventuroso/Dev/Python/sgc-env/lib/python3.10/site-packages/djongo/sql2mongo/operators.py", line 454, in _statement2ops
    op = self._token2op(tok, statement)
  File "/home/muriloventuroso/Dev/Python/sgc-env/lib/python3.10/site-packages/djongo/sql2mongo/operators.py", line 421, in _token2op
    op = CmpOp(tok, self.query)
  File "/home/muriloventuroso/Dev/Python/sgc-env/lib/python3.10/site-packages/djongo/sql2mongo/operators.py", line 535, in __init__
    self._operator = OPERATOR_MAP[self.statement.token_next(0)[1].value]
KeyError: 'iLIKE'

found same issue with iexact

I think this problem may be caused by sqlparse version upgrade.

@gregorlulichtec this seems related to the upgrade of sqlparse (49ac474), it is not working on master but it works with djongo 1.3.6, can you take a look on it, please?

I did some research on this. Since sqlparse==0.3.1, LIKE, iLIKE, NOT LIKE, and NOT ILIKE are parsed as comparison operators and not as simple keywords anymore, see andialbrecht/sqlparse#525.

This means, they are not detected anymore here:

elif tok.match(tokens.Keyword, 'LIKE'):
op = LikeOp(**kw)
elif tok.match(tokens.Keyword, 'iLIKE'):
op = iLikeOp(**kw)

The solution seems to be integrating LikeOp and iLikeOp into CmpOp.