dimagi / django-cte

Common Table Expressions (CTE) for Django
Other
334 stars 46 forks source link

CTECompiler incompatible with GIS lookups #26

Closed SebCorbin closed 2 years ago

SebCorbin commented 3 years ago

When using GIS lookups, the CTECompiler cast Subquery params to tuple, making it impossible to spatially filter a field provided by a Subquery.

I get this error :

Traceback (most recent call last):
[...]
  File "lib/python3.8/site-packages/django/db/models/query.py", line 274, in __iter__
    self._fetch_all()
  File "lib/python3.8/site-packages/django/db/models/query.py", line 1242, in _fetch_all
    self._result_cache = list(self._iterable_class(self))
  File "lib/python3.8/site-packages/django/db/models/query.py", line 55, in __iter__
    results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)
  File "lib/python3.8/site-packages/django/db/models/sql/compiler.py", line 1127, in execute_sql
    sql, params = self.as_sql()
  File "lib/python3.8/site-packages/django/db/models/sql/compiler.py", line 489, in as_sql
    where, w_params = self.compile(self.where) if self.where is not None else ("", [])
  File "lib/python3.8/site-packages/django/db/models/sql/compiler.py", line 405, in compile
    sql, params = node.as_sql(self, self.connection)
  File "lib/python3.8/site-packages/django/db/models/sql/where.py", line 81, in as_sql
    sql, params = compiler.compile(child)
  File "lib/python3.8/site-packages/django/db/models/sql/compiler.py", line 405, in compile
    sql, params = node.as_sql(self, self.connection)
  File "lib/python3.8/site-packages/django/contrib/gis/db/models/lookups.py", line 78, in as_sql
    lhs_sql, sql_params = self.process_lhs(compiler, connection)
AttributeError: 'tuple' object has no attribute 'extend'

When modifying CTECompiler.generate_sql to return " ".join(sql), params instead of return " ".join(sql), tuple(params), error is gone and queryset works well

This error happens only on Django < 3.1.4 as the handling of params has been strengthened since https://github.com/django/django/commit/0290e01d5a202507e9a71294ac9cb31725c951bb

Edit: So it's just that the core compiler was not consistent with the signature (see https://code.djangoproject.com/ticket/31002), what do you suggest? Should we fix it in django-cte ?

millerdev commented 2 years ago

@SebCorbin is this still an issue with Django 3.2+?

SebCorbin commented 2 years ago

It is not