Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Possible missing table ref in select_related()? #25

Open
tfh-cri opened this issue Sep 9, 2024 · 0 comments
Open

Possible missing table ref in select_related()? #25

tfh-cri opened this issue Sep 9, 2024 · 0 comments

Comments

@tfh-cri
Copy link

tfh-cri commented Sep 9, 2024

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?)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant