djongo icon indicating copy to clipboard operation
djongo copied to clipboard

Unable to parse Boolean SQL condition

Open magmonium opened this issue 3 years ago • 7 comments

description

Djongo unable to parse following where condition for Boolean value.

'SELECT "company_config"."_id", "company_config"."company", "company_config"."user", "company_config"."visitedAt", "company_config"."isWatching" FROM "company_config" WHERE ("company_config"."isWatching"

Python script


from djongo import models

  class Config(models.Model):

    class Meta:
        unique_together = (('company', 'user'))

    _id = models.ObjectIdField()
    company =  models.CharField(max_length=20)
    user = models.CharField(max_length=20, default='dibyendu')
    visitedAt = models.DateField(default=None)
    isWatching = models.BooleanField(default=False)

  Config.objects.filter(isWatching=True, user="dibyendu")

Traceback


raise SQLDecodeError
djongo.exceptions.SQLDecodeError:

        Keyword: None
        Sub SQL: None
        FAILED SQL: ('SELECT "company_config"."_id", "company_config"."company", "company_config"."user", "company_config"."visitedAt", "company_config"."isWatching" FROM "company_config" WHERE ("company_config"."isWatching" AND "company_config"."user" = %(0)s)',)
        Params: (('dibyendu',),)
        Version: 1.3.6

magmonium avatar Jul 04 '21 02:07 magmonium

@magmonium I also have the same problem.

The solution:

Config.objects.filter(isWatching__in=[True], user="dibyendu")

But it is just a temporary solution because many libraries use filter syntax like yours

ngocngoan avatar Jul 09 '21 07:07 ngocngoan

@ngocngoan thanks for your reply with workaround. I am going to use this solution till this request is addressed. :)

magmonium avatar Jul 10 '21 08:07 magmonium

Seems like the issue has been raised from a year ago and still not fixed! https://github.com/nesdis/djongo/issues/465

fduraibi avatar Aug 01 '21 09:08 fduraibi

Here is a monkey patch approach that is less interruptive:

from djongo.base import DatabaseWrapper
from djongo.operations import DatabaseOperations

class PatchedDatabaseOperations(DatabaseOperations):

    def conditional_expression_supported_in_where_clause(self, expression):
        return False


DatabaseWrapper.ops_class = PatchedDatabaseOperations

import this file in either manage.py or settings.py and the issue will be solved.

Someone can submit a PR based on the above solution.

frostming avatar Aug 04 '21 08:08 frostming

Why is this issue closed? I'm still having this issue.

redigaffi avatar Sep 18 '21 16:09 redigaffi

Issue still present, had to use "__in=[True]" workaround. Could we get this reopened?

Dkr0l avatar Sep 28 '21 16:09 Dkr0l

It is 2022 - still facing this issue. In the past three hours, I have explored the corners of dark webs and contemplated quitting my career.

That said, thank you @ngocngoan

ajwad-shaikh avatar Oct 27 '22 14:10 ajwad-shaikh