django-relativity icon indicating copy to clipboard operation
django-relativity copied to clipboard

Possible missing table ref in `select_related()`?

Open tfh-cri opened this issue 1 year ago • 0 comments

I'm not sure if I'm misunderstanding how this is all meant to work, or if this is actually a bug.

I've created a failing (well, currently XFAIL) test on a fork here

The models are a rough proxy for my actual intended use, which is to have a Relationship that can look up temporal relationships for things belonging to projects.

Models: https://github.com/tfh-cri/django-relativity/blob/d9827dfc149eaf051e210d738098bebf5dfe935a/tests/models.py#L227

The actual exception is something like:

-> % PYTHONPATH=. DJANGO_SETTINGS_MODULE=tests.settings .venv/bin/django-admin test -k MoreTests
Found 3 test(s).
Creating test database for alias 'default'...
System check identified no issues (0 silenced).
..E
======================================================================
ERROR: test_forward_select_related (tests.tests.MoreTests.test_forward_select_related)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/tfh/sources/django-relativity/.venv/lib/python3.12/site-packages/django/db/backends/utils.py", line 105, in _execute
    return self.cursor.execute(sql, params)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/tfh/sources/django-relativity/.venv/lib/python3.12/site-packages/django/db/backends/sqlite3/base.py", line 354, in execute
    return super().execute(query, params)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
sqlite3.OperationalError: no such column: T4.id

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/tfh/sources/django-relativity/tests/tests.py", line 454, in test_forward_select_related
    usage = ItemUsage.objects.select_related('used_in', 'assigned_to').get(pk=1)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/tfh/sources/django-relativity/.venv/lib/python3.12/site-packages/django/db/models/query.py", line 645, in get
    num = len(clone)
          ^^^^^^^^^^
  File "/home/tfh/sources/django-relativity/.venv/lib/python3.12/site-packages/django/db/models/query.py", line 382, in __len__
    self._fetch_all()
  File "/home/tfh/sources/django-relativity/.venv/lib/python3.12/site-packages/django/db/models/query.py", line 1928, in _fetch_all
    self._result_cache = list(self._iterable_class(self))
                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/tfh/sources/django-relativity/.venv/lib/python3.12/site-packages/django/db/models/query.py", line 91, in __iter__
    results = compiler.execute_sql(
              ^^^^^^^^^^^^^^^^^^^^^
  File "/home/tfh/sources/django-relativity/.venv/lib/python3.12/site-packages/django/db/models/sql/compiler.py", line 1574, in execute_sql
    cursor.execute(sql, params)
  File "/home/tfh/sources/django-relativity/.venv/lib/python3.12/site-packages/django/db/backends/utils.py", line 122, in execute
    return super().execute(sql, params)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/tfh/sources/django-relativity/.venv/lib/python3.12/site-packages/django/db/backends/utils.py", line 79, in execute
    return self._execute_with_wrappers(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/tfh/sources/django-relativity/.venv/lib/python3.12/site-packages/django/db/backends/utils.py", line 92, in _execute_with_wrappers
    return executor(sql, params, many, context)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/tfh/sources/django-relativity/.venv/lib/python3.12/site-packages/django/db/backends/utils.py", line 100, in _execute
    with self.db.wrap_database_errors:
  File "/home/tfh/sources/django-relativity/.venv/lib/python3.12/site-packages/django/db/utils.py", line 91, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "/home/tfh/sources/django-relativity/.venv/lib/python3.12/site-packages/django/db/backends/utils.py", line 105, in _execute
    return self.cursor.execute(sql, params)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/tfh/sources/django-relativity/.venv/lib/python3.12/site-packages/django/db/backends/sqlite3/base.py", line 354, in execute
    return super().execute(query, params)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
django.db.utils.OperationalError: no such column: T4.id

----------------------------------------------------------------------
Ran 3 tests in 0.012s

And the query it is producing is:

-- (Pdb) print(sqlparse.format(query, reindent=True))
SELECT "tests_itemusage"."id",
       "tests_itemusage"."item_id",
       "tests_itemusage"."used_at",
       "tests_itemassignment"."id",
       "tests_itemassignment"."item_id",
       "tests_itemassignment"."project_id",
       "tests_itemassignment"."assigned_from",
       "tests_itemassignment"."assigned_until",
       "tests_project"."id",
       "tests_project"."name"
FROM "tests_itemusage"
LEFT OUTER JOIN "tests_itemassignment" ON ((("tests_itemassignment"."assigned_from" <= ("tests_itemusage"."used_at")
                                             AND "tests_itemassignment"."assigned_until" >= ("tests_itemusage"."used_at")
                                             AND "tests_itemassignment"."item_id" = ("tests_itemusage"."item_id"))))
LEFT OUTER JOIN "tests_project" ON (("T4"."id" = ("tests_itemassignment"."id")))
WHERE "tests_itemusage"."id" = ?
LIMIT 21

Which does appear to be referencing the T4 table but not actually naming it anywhere.

(unrelatedly, there are some fails in the existing tests for django 5, which I fixed here, but could pull out into a separate PR if useful?)

tfh-cri avatar Sep 09 '24 13:09 tfh-cri