djongo
djongo copied to clipboard
Unable to parse Boolean SQL condition
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 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 thanks for your reply with workaround. I am going to use this solution till this request is addressed. :)
Seems like the issue has been raised from a year ago and still not fixed! https://github.com/nesdis/djongo/issues/465
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.
Why is this issue closed? I'm still having this issue.
Issue still present, had to use "__in=[True]" workaround. Could we get this reopened?
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