bradmontgomery / django-querycount

Middleware that Prints the number of DB queries to the runserver console.
MIT License
392 stars 33 forks source link

WHERE_CLAUSE_REGEX bad escape problem #32

Open ckcollab opened 1 year ago

ckcollab commented 1 year ago

First of all: thanks so much for this module! I use it a shitload and it, basically, never lets me down. Appreciate it :)

I'm getting this little problemo with Django 4.2, Python 3.11, Psycopg 3.1.8:

Internal Server Error: /api/auth/login/
Traceback (most recent call last):
  File "/usr/local/lib/python3.11/site-packages/django/core/handlers/exception.py", line 55, in inner
    response = get_response(request)
               ^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/django/utils/deprecation.py", line 136, in __call__
    response = self.process_response(request, response)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/querycount/middleware.py", line 117, in process_response
    self._count_queries("response")
  File "/usr/local/lib/python3.11/site-packages/querycount/middleware.py", line 85, in _count_queries
    sql = self.WHERE_CLAUSE_REGEX.sub(
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/re/__init__.py", line 317, in _subx
    template = _compile_repl(template, pattern)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/re/__init__.py", line 308, in _compile_repl
    return _parser.parse_template(repl, pattern)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/re/_parser.py", line 1078, in parse_template
    raise s.error('bad escape %s' % this, len(this)) from None
re.error: bad escape \x at position 55

Not exactly sure what's causing this, but thought it might be worth posting an issue about. Maybe data changed in some way between Django 4.1 and 4.2?

ptwobrussell commented 1 year ago

I hit a nearly identical issue with a similar stack trace after a routine dependencies upgrade -- just a different escape character.

A common thread is that I also went from Django 4.1 => 4.2, so perhaps that is worth a closer look.

One thing that jumps out at me in the Django 4.2 release notes is that Django 4.2 requires an update to Postgres 12+. Perhaps some of the database dependencies that are triggered as part of that major version update are at the root of this issue.

https://docs.djangoproject.com/en/4.2/releases/4.2/#database-backend-api

Shivaank121 commented 1 year ago

Getting similar issue with Django 3.2, GeoDjango, Postgres with postgis extension for only queries like this:

from django.contrib.gis.geos import Point
from django.contrib.gis.measure import Distance  

lat = 52.5
lng = 1.0
radius = 10
point = Point(lng, lat)    
Place.objects.filter(location__distance_lt=(point, Distance(km=radius))) 
Screen Shot 2023-10-06 at 6 55 04 PM

Other ORM queries working fine.

https://gis.stackexchange.com/questions/141533/geodjango-find-all-points-within-radius