Djongo + Django + MongoDB Atlas DatabaseError when trying to reset password
Asked Answered
P

1

7

So I'm trying to create a password reset link for a website that is running on Django and uses Djongo. Whenever I enter a password and click on the reset password button, this error occurs:

Environment:


Request Method: POST
Request URL: http://localhost:8000/password-reset/

Django Version: 3.1.4
Python Version: 3.8.3
Installed Applications:
['blog.apps.BlogConfig',
 'users.apps.UsersConfig',
 'crispy_forms',
 'django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'django_cleanup']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']



Traceback (most recent call last):
  File "C:\Users\Harry\anaconda3\envs\agrifarm\lib\site-packages\djongo\cursor.py", line 51, in execute
    self.result = Query(
  File "C:\Users\Harry\anaconda3\envs\agrifarm\lib\site-packages\djongo\sql2mongo\query.py", line 783, in __init__
    self._query = self.parse()
  File "C:\Users\Harry\anaconda3\envs\agrifarm\lib\site-packages\djongo\sql2mongo\query.py", line 875, in parse
    raise e
  File "C:\Users\Harry\anaconda3\envs\agrifarm\lib\site-packages\djongo\sql2mongo\query.py", line 856, in parse
    return handler(self, statement)
  File "C:\Users\Harry\anaconda3\envs\agrifarm\lib\site-packages\djongo\sql2mongo\query.py", line 932, in _select
    return SelectQuery(self.db, self.connection_properties, sm, self._params)
  File "C:\Users\Harry\anaconda3\envs\agrifarm\lib\site-packages\djongo\sql2mongo\query.py", line 116, in __init__
    super().__init__(*args)
  File "C:\Users\Harry\anaconda3\envs\agrifarm\lib\site-packages\djongo\sql2mongo\query.py", line 62, in __init__
    self.parse()
  File "C:\Users\Harry\anaconda3\envs\agrifarm\lib\site-packages\djongo\sql2mongo\query.py", line 152, in parse
    self.where = WhereConverter(self, statement)
  File "C:\Users\Harry\anaconda3\envs\agrifarm\lib\site-packages\djongo\sql2mongo\converters.py", line 27, in __init__
    self.parse()
  File "C:\Users\Harry\anaconda3\envs\agrifarm\lib\site-packages\djongo\sql2mongo\converters.py", line 119, in parse
    self.op = WhereOp(
  File "C:\Users\Harry\anaconda3\envs\agrifarm\lib\site-packages\djongo\sql2mongo\operators.py", line 476, in __init__
    self.evaluate()
  File "C:\Users\Harry\anaconda3\envs\agrifarm\lib\site-packages\djongo\sql2mongo\operators.py", line 465, in evaluate
    op.evaluate()
  File "C:\Users\Harry\anaconda3\envs\agrifarm\lib\site-packages\djongo\sql2mongo\operators.py", line 465, in evaluate
    op.evaluate()
  File "C:\Users\Harry\anaconda3\envs\agrifarm\lib\site-packages\djongo\sql2mongo\operators.py", line 279, in evaluate
    raise SQLDecodeError

The above exception (

    Keyword: None
    Sub SQL: None
    FAILED SQL: ('SELECT "auth_user"."id", "auth_user"."password", "auth_user"."last_login", "auth_user"."is_superuser", "auth_user"."username", "auth_user"."first_name", "auth_user"."last_name", "auth_user"."email", "auth_user"."is_staff", "auth_user"."is_active", "auth_user"."date_joined" FROM "auth_user" WHERE ("auth_user"."email" iLIKE %(0)s AND "auth_user"."is_active")',)
    Params: (('[email protected]',),)
    Version: 1.3.3) was the direct cause of the following exception:
  File "C:\Users\Harry\anaconda3\envs\agrifarm\lib\site-packages\django\db\backends\utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
  File "C:\Users\Harry\anaconda3\envs\agrifarm\lib\site-packages\djongo\cursor.py", line 59, in execute
    raise db_exe from e

The above exception () was the direct cause of the following exception:
  File "C:\Users\Harry\anaconda3\envs\agrifarm\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
    response = get_response(request)
  File "C:\Users\Harry\anaconda3\envs\agrifarm\lib\site-packages\django\core\handlers\base.py", line 179, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\Users\Harry\anaconda3\envs\agrifarm\lib\site-packages\django\views\generic\base.py", line 70, in view
    return self.dispatch(request, *args, **kwargs)
  File "C:\Users\Harry\anaconda3\envs\agrifarm\lib\site-packages\django\utils\decorators.py", line 43, in _wrapper
    return bound_method(*args, **kwargs)
  File "C:\Users\Harry\anaconda3\envs\agrifarm\lib\site-packages\django\utils\decorators.py", line 130, in _wrapped_view
    response = view_func(request, *args, **kwargs)
  File "C:\Users\Harry\anaconda3\envs\agrifarm\lib\site-packages\django\contrib\auth\views.py", line 222, in dispatch
    return super().dispatch(*args, **kwargs)
  File "C:\Users\Harry\anaconda3\envs\agrifarm\lib\site-packages\django\views\generic\base.py", line 98, in dispatch
    return handler(request, *args, **kwargs)
  File "C:\Users\Harry\anaconda3\envs\agrifarm\lib\site-packages\django\views\generic\edit.py", line 142, in post
    return self.form_valid(form)
  File "C:\Users\Harry\anaconda3\envs\agrifarm\lib\site-packages\django\contrib\auth\views.py", line 235, in form_valid
    form.save(**opts)
  File "C:\Users\Harry\anaconda3\envs\agrifarm\lib\site-packages\django\contrib\auth\forms.py", line 311, in save
    for user in self.get_users(email):
  File "C:\Users\Harry\anaconda3\envs\agrifarm\lib\site-packages\django\contrib\auth\forms.py", line 287, in get_users
    return (
  File "C:\Users\Harry\anaconda3\envs\agrifarm\lib\site-packages\django\db\models\query.py", line 287, in __iter__
    self._fetch_all()
  File "C:\Users\Harry\anaconda3\envs\agrifarm\lib\site-packages\django\db\models\query.py", line 1308, in _fetch_all
    self._result_cache = list(self._iterable_class(self))
  File "C:\Users\Harry\anaconda3\envs\agrifarm\lib\site-packages\django\db\models\query.py", line 53, in __iter__
    results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)
  File "C:\Users\Harry\anaconda3\envs\agrifarm\lib\site-packages\django\db\models\sql\compiler.py", line 1156, in execute_sql
    cursor.execute(sql, params)
  File "C:\Users\Harry\anaconda3\envs\agrifarm\lib\site-packages\django\db\backends\utils.py", line 98, in execute
    return super().execute(sql, params)
  File "C:\Users\Harry\anaconda3\envs\agrifarm\lib\site-packages\django\db\backends\utils.py", line 66, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
  File "C:\Users\Harry\anaconda3\envs\agrifarm\lib\site-packages\django\db\backends\utils.py", line 75, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "C:\Users\Harry\anaconda3\envs\agrifarm\lib\site-packages\django\db\backends\utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
  File "C:\Users\Harry\anaconda3\envs\agrifarm\lib\site-packages\django\db\utils.py", line 90, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "C:\Users\Harry\anaconda3\envs\agrifarm\lib\site-packages\django\db\backends\utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
  File "C:\Users\Harry\anaconda3\envs\agrifarm\lib\site-packages\djongo\cursor.py", line 59, in execute
    raise db_exe from e

Exception Type: DatabaseError at /password-reset/
Exception Value: 

So far I can't seem to find anyone having the same problem.
I think the error means it can't find the email I entered in the database. But when I check in the MongoDB Atlas Collection tab, under auth_user group, I have multiple entries with the same email so I can't figure out what's going on.

Anyone has come across this problem before?

Parable answered 14/12, 2020 at 9:22 Comment(0)
T
6

The error says that Djongo's SQL parser expects 2 values and an operator between them in a search predicate. It fails to handle boolean columns used directly in the Django's query builder - the highlighted part of the WHERE statement:

WHERE ("auth_user"."email" iLIKE %(0)s AND "auth_user"."is_active")',)

This exact issue reported in https://github.com/nesdis/djongo/issues/543

And a tactical workaround proposed in the main issue https://github.com/nesdis/djongo/issues/562#issuecomment-892486144 suggests monkey-pathing the db wrapper:

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
Towland answered 17/11, 2021 at 13:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.