This is the ORM code that causes the Postgres error:
resultsqs = (
res.order_by(*all_distinct_fields)
.distinct(*all_distinct_fields)
.values_list(*all_fields)
)
This is all_dicstinct_fields:
This is what's in all_distinct_fields: `['name', 'pk', 'labels__peak_group__name', 'labels__element', 'labels__pk', 'msrun_sample__sample__animal__infusate__name', 'msrun_sample__sample__animal__infusate__tracer_links__tracer__name', 'msrun_sample__sample__animal__infusate__tracer_links__concentration', 'msrun_sample__sample__animal__infusate__tracer_links__pk', 'msrun_sample__sample__animal__infusate__tracers__name', 'msrun_sample__sample__animal__infusate__tracers__pk', 'msrun_sample__sample__animal__infusate__tracers__compound__name', 'msrun_sample__sample__animal__infusate__tracers__compound__pk', 'compounds__name', 'compounds__pk', 'compounds__synonyms__compound__name', 'compounds__synonyms__name', 'compounds__synonyms__pk', 'msrun_sample__sample__animal__studies__name', 'msrun_sample__sample__animal__studies__pk', 'msrun_sample__sample__msrun_samples__ms_data_file__filename', 'msrun_sample__sample__msrun_samples__ms_data_file__data_type__name', 'msrun_sample__sample__msrun_samples__ms_data_file__data_format__name', 'msrun_sample__sample__msrun_samples__ms_data_file__imported_timestamp', 'msrun_sample__sample__msrun_samples__ms_data_file__pk', 'msrun_sample__sample__msrun_samples__ms_raw_file__filename', 'msrun_sample__sample__msrun_samples__ms_raw_file__data_type__name', 'msrun_sample__sample__msrun_samples__ms_raw_file__data_format__name', 'msrun_sample__sample__msrun_samples__ms_raw_file__imported_timestamp', 'msrun_sample__sample__msrun_samples__ms_raw_file__pk']`
This is all_fields:
all_fields: ['name', 'pk', 'labels__peak_group__name', 'labels__element', 'labels__pk', 'msrun_sample__sample__animal__infusate__name', 'msrun_sample__sample__animal__infusate__tracer_links__tracer__name', 'msrun_sample__sample__animal__infusate__tracer_links__concentration', 'msrun_sample__sample__animal__infusate__tracer_links__pk', 'msrun_sample__sample__animal__infusate__tracers__name', 'msrun_sample__sample__animal__infusate__tracers__pk', 'msrun_sample__sample__animal__infusate__tracers__compound__name', 'msrun_sample__sample__animal__infusate__tracers__compound__pk', 'compounds__name', 'compounds__pk', 'compounds__synonyms__compound__name', 'compounds__synonyms__name', 'compounds__synonyms__pk', 'msrun_sample__sample__animal__studies__name', 'msrun_sample__sample__animal__studies__pk', 'msrun_sample__sample__msrun_samples__ms_data_file__filename', 'msrun_sample__sample__msrun_samples__ms_data_file__data_type__name', 'msrun_sample__sample__msrun_samples__ms_data_file__data_format__name', 'msrun_sample__sample__msrun_samples__ms_data_file__imported_timestamp', 'msrun_sample__sample__msrun_samples__ms_data_file__pk', 'msrun_sample__sample__msrun_samples__ms_raw_file__filename', 'msrun_sample__sample__msrun_samples__ms_raw_file__data_type__name', 'msrun_sample__sample__msrun_samples__ms_raw_file__data_format__name', 'msrun_sample__sample__msrun_samples__ms_raw_file__imported_timestamp', 'msrun_sample__sample__msrun_samples__ms_raw_file__pk', 'msrun_sample__sample__animal__name', 'peak_data__labels__element', 'compounds__name', 'msrun_sample__sample__name', 'msrun_sample__sample__tissue__name', 'msrun_sample__sample__animal__infusate__tracers__compound__name', 'msrun_sample__sample__animal__studies__name', 'msrun_sample__sample__animal__feeding_status', 'msrun_sample__sample__animal__infusion_rate', 'msrun_sample__sample__animal__infusate__tracers__compound__name', 'msrun_sample__sample__animal__infusate__tracer_links__concentration']
This might be the most illustrative prompt, after having added some debug prints that ChatGPT suggested:
I commented out the ordering in ArchiveFile and still get an error:
```
SQL: SELECT DISTINCT ON ("DataRepo_peakgroup"."name", "DataRepo_peakgroup"."id", T15."name", "DataRepo_peakgrouplabel"."element", "DataRepo_peakgrouplabel"."id", "DataRepo_infusate"."name", "DataRepo_tracer"."name", "DataRepo_infusatetracer"."concentration", "DataRepo_infusatetracer"."id", "DataRepo_tracer"."name", "DataRepo_infusatetracer"."tracer_id", T11."name", "DataRepo_tracer"."compound_id", "DataRepo_compound"."name", "DataRepo_peakgroup_compounds"."compound_id", T17."name", "DataRepo_compoundsynonym"."name", "DataRepo_compoundsynonym"."name", "DataRepo_study"."name", "DataRepo_animal_studies"."study_id", T18."ms_data_file_id", T18."ms_raw_file_id") "DataRepo_peakgroup"."name" AS "name", "DataRepo_peakgroup"."id" AS "pk", T15."name" AS "labels__peak_group__name", "DataRepo_peakgrouplabel"."element" AS "labels__element", "DataRepo_peakgrouplabel"."id" AS "labels__pk", "DataRepo_infusate"."name" AS "msrun_sample__sample__animal__infusate__name", "DataRepo_tracer"."name" AS "msrun_sample__sample__animal__infusate__tracer_links__tracer__name", "DataRepo_infusatetracer"."concentration" AS "msrun_sample__sample__animal__infusate__tracer_links__concentration", "DataRepo_infusatetracer"."id" AS "msrun_sample__sample__animal__infusate__tracer_links__pk", "DataRepo_tracer"."name" AS "msrun_sample__sample__animal__infusate__tracers__name", "DataRepo_infusatetracer"."tracer_id" AS "msrun_sample__sample__animal__infusate__tracers__pk", T11."name" AS "msrun_sample__sample__animal__infusate__tracers__compound__name", "DataRepo_tracer"."compound_id" AS "msrun_sample__sample__animal__infusate__tracers__compound__pk", "DataRepo_compound"."name" AS "compounds__name", "DataRepo_peakgroup_compounds"."compound_id" AS "compounds__pk", T17."name" AS "compounds__synonyms__compound__name", "DataRepo_compoundsynonym"."name" AS "compounds__synonyms__name", "DataRepo_compoundsynonym"."name" AS "compounds__synonyms__pk", "DataRepo_study"."name" AS "msrun_sample__sample__animal__studies__name", "DataRepo_animal_studies"."study_id" AS "msrun_sample__sample__animal__studies__pk", T18."ms_data_file_id" AS "msrun_sample__sample__msrun_samples__ms_data_file__pk", T18."ms_raw_file_id" AS "msrun_sample__sample__msrun_samples__ms_raw_file__pk", "DataRepo_animal"."name" AS "msrun_sample__sample__animal__name", "DataRepo_peakdatalabel"."element" AS "peak_data__labels__element", "DataRepo_compound"."name" AS "compounds__name1", "DataRepo_sample"."name" AS "msrun_sample__sample__name", "DataRepo_tissue"."name" AS "msrun_sample__sample__tissue__name", T11."name" AS "msrun_sample__sample__animal__infusate__tracers__compound__name1", "DataRepo_study"."name" AS "msrun_sample__sample__animal__studies__name1", "DataRepo_animal"."feeding_status" AS "msrun_sample__sample__animal__feeding_status", "DataRepo_animal"."infusion_rate" AS "msrun_sample__sample__animal__infusion_rate", T11."name" AS "msrun_sample__sample__animal__infusate__tracers__compound__name2", "DataRepo_infusatetracer"."concentration" AS "msrun_sample__sample__animal__infusate__tracer_links__concentration2" FROM "DataRepo_peakgroup" INNER JOIN "DataRepo_msrunsample" ON ("DataRepo_peakgroup"."msrun_sample_id" = "DataRepo_msrunsample"."id") INNER JOIN "DataRepo_sample" ON ("DataRepo_msrunsample"."sample_id" = "DataRepo_sample"."id") INNER JOIN "DataRepo_tissue" ON ("DataRepo_sample"."tissue_id" = "DataRepo_tissue"."id") LEFT OUTER JOIN "DataRepo_peakgroup_compounds" ON ("DataRepo_peakgroup"."id" = "DataRepo_peakgroup_compounds"."peakgroup_id") LEFT OUTER JOIN "DataRepo_compound" ON ("DataRepo_peakgroup_compounds"."compound_id" = "DataRepo_compound"."id") INNER JOIN "DataRepo_animal" ON ("DataRepo_sample"."animal_id" = "DataRepo_animal"."id") LEFT OUTER JOIN "DataRepo_infusate" ON ("DataRepo_animal"."infusate_id" = "DataRepo_infusate"."id") LEFT OUTER JOIN "DataRepo_infusatetracer" ON ("DataRepo_infusate"."id" = "DataRepo_infusatetracer"."infusate_id") LEFT OUTER JOIN "DataRepo_tracer" ON ("DataRepo_infusatetracer"."tracer_id" = "DataRepo_tracer"."id") LEFT OUTER JOIN "DataRepo_compound" T11 ON ("DataRepo_tracer"."compound_id" = T11."id") LEFT OUTER JOIN "DataRepo_animal_studies" ON ("DataRepo_animal"."id" = "DataRepo_animal_studies"."animal_id") LEFT OUTER JOIN "DataRepo_study" ON ("DataRepo_animal_studies"."study_id" = "DataRepo_study"."id") LEFT OUTER JOIN "DataRepo_peakgrouplabel" ON ("DataRepo_peakgroup"."id" = "DataRepo_peakgrouplabel"."peak_group_id") LEFT OUTER JOIN "DataRepo_peakgroup" T15 ON ("DataRepo_peakgrouplabel"."peak_group_id" = T15."id") LEFT OUTER JOIN "DataRepo_compoundsynonym" ON ("DataRepo_compound"."id" = "DataRepo_compoundsynonym"."compound_id") LEFT OUTER JOIN "DataRepo_compound" T17 ON ("DataRepo_compoundsynonym"."compound_id" = T17."id") LEFT OUTER JOIN "DataRepo_msrunsample" T18 ON ("DataRepo_sample"."id" = T18."sample_id") LEFT OUTER JOIN "DataRepo_peakdata" ON ("DataRepo_peakgroup"."id" = "DataRepo_peakdata"."peak_group_id") LEFT OUTER JOIN "DataRepo_peakdatalabel" ON ("DataRepo_peakdata"."id" = "DataRepo_peakdatalabel"."peak_data_id") WHERE UPPER("DataRepo_tissue"."name"::text) = UPPER(Brain) ORDER BY 1 ASC, 2 ASC, 3 ASC, 4 ASC, 5 ASC, 6 ASC, 7 ASC, 8 ASC, 9 ASC, 10 ASC, 11 ASC, 12 ASC, 13 ASC, 14 ASC, 15 ASC, 16 ASC, 17 ASC, 18 ASC, 19 ASC, 20 ASC, 21 ASC, 22 ASC
all_fields: ['name', 'pk', 'labels__peak_group__name', 'labels__element', 'labels__pk', 'msrun_sample__sample__animal__infusate__name', 'msrun_sample__sample__animal__infusate__tracer_links__tracer__name', 'msrun_sample__sample__animal__infusate__tracer_links__concentration', 'msrun_sample__sample__animal__infusate__tracer_links__pk', 'msrun_sample__sample__animal__infusate__tracers__name', 'msrun_sample__sample__animal__infusate__tracers__pk', 'msrun_sample__sample__animal__infusate__tracers__compound__name', 'msrun_sample__sample__animal__infusate__tracers__compound__pk', 'compounds__name', 'compounds__pk', 'compounds__synonyms__compound__name', 'compounds__synonyms__name', 'compounds__synonyms__pk', 'msrun_sample__sample__animal__studies__name', 'msrun_sample__sample__animal__studies__pk', 'msrun_sample__sample__msrun_samples__ms_data_file__pk', 'msrun_sample__sample__msrun_samples__ms_raw_file__pk', 'msrun_sample__sample__animal__name', 'peak_data__labels__element', 'compounds__name', 'msrun_sample__sample__name', 'msrun_sample__sample__tissue__name', 'msrun_sample__sample__animal__infusate__tracers__compound__name', 'msrun_sample__sample__animal__studies__name', 'msrun_sample__sample__animal__feeding_status', 'msrun_sample__sample__animal__infusion_rate', 'msrun_sample__sample__animal__infusate__tracers__compound__name', 'msrun_sample__sample__animal__infusate__tracer_links__concentration']
DISTINCT FIELDS:
['"DataRepo_peakgroup"."name"', '"DataRepo_peakgroup"."id"', 'T15."name"', '"DataRepo_peakgrouplabel"."element"', '"DataRepo_peakgrouplabel"."id"', '"DataRepo_infusate"."name"', '"DataRepo_tracer"."name"', '"DataRepo_infusatetracer"."concentration"', '"DataRepo_infusatetracer"."id"', '"DataRepo_tracer"."name"', '"DataRepo_infusatetracer"."tracer_id"', 'T11."name"', '"DataRepo_tracer"."compound_id"', '"DataRepo_compound"."name"', '"DataRepo_peakgroup_compounds"."compound_id"', 'T17."name"', '"DataRepo_compoundsynonym"."name"', '"DataRepo_compoundsynonym"."name"', '"DataRepo_study"."name"', '"DataRepo_animal_studies"."study_id"', 'T18."ms_data_file_id"', 'T18."ms_raw_file_id"']
[[], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], []]
order_by:
('name',
'pk',
'labels__peak_group__name',
'labels__element',
'labels__pk',
'msrun_sample__sample__animal__infusate__name',
'msrun_sample__sample__animal__infusate__tracer_links__tracer__name',
'msrun_sample__sample__animal__infusate__tracer_links__concentration',
'msrun_sample__sample__animal__infusate__tracer_links__pk',
'msrun_sample__sample__animal__infusate__tracers__name',
'msrun_sample__sample__animal__infusate__tracers__pk',
'msrun_sample__sample__animal__infusate__tracers__compound__name',
'msrun_sample__sample__animal__infusate__tracers__compound__pk',
'compounds__name',
'compounds__pk',
'compounds__synonyms__compound__name',
'compounds__synonyms__name',
'compounds__synonyms__pk',
'msrun_sample__sample__animal__studies__name',
'msrun_sample__sample__animal__studies__pk',
'msrun_sample__sample__msrun_samples__ms_data_file__pk',
'msrun_sample__sample__msrun_samples__ms_raw_file__pk')
distinct_fields:
('name',
'pk',
'labels__peak_group__name',
'labels__element',
'labels__pk',
'msrun_sample__sample__animal__infusate__name',
'msrun_sample__sample__animal__infusate__tracer_links__tracer__name',
'msrun_sample__sample__animal__infusate__tracer_links__concentration',
'msrun_sample__sample__animal__infusate__tracer_links__pk',
'msrun_sample__sample__animal__infusate__tracers__name',
'msrun_sample__sample__animal__infusate__tracers__pk',
'msrun_sample__sample__animal__infusate__tracers__compound__name',
'msrun_sample__sample__animal__infusate__tracers__compound__pk',
'compounds__name',
'compounds__pk',
'compounds__synonyms__compound__name',
'compounds__synonyms__name',
'compounds__synonyms__pk',
'msrun_sample__sample__animal__studies__name',
'msrun_sample__sample__animal__studies__pk',
'msrun_sample__sample__msrun_samples__ms_data_file__pk',
'msrun_sample__sample__msrun_samples__ms_raw_file__pk')
ETEST TIME: DataRepo.tests.views.search.formats.test_dataformat_group.FormatGroupTests.test_get_query_stats_full: 0.012
======================================================================
ERROR: test_get_query_stats_full (DataRepo.tests.views.search.formats.test_dataformat_group.FormatGroupTests.test_get_query_stats_full)
Test that get_query_stats returns a correct stats structure
----------------------------------------------------------------------
Traceback (most recent call last):
File "/Users/rleach/PROJECT-LOCAL/TRACEBASE/tracebase3/.venv/lib/python3.11/site-packages/django/db/backends/utils.py", line 105, in _execute
return self.cursor.execute(sql, params)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
psycopg2.errors.InvalidColumnReference: SELECT DISTINCT ON expressions must match initial ORDER BY expressions
LINE 1: ...o_infusatetracer"."id", "DataRepo_tracer"."name", "DataRepo_...
^
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/Users/rleach/PROJECT-LOCAL/TRACEBASE/tracebase3/DataRepo/views/search/formats/dataformat_group.py", line 725, in get_query_stats
for rec in resultsqs.all():
File "/Users/rleach/PROJECT-LOCAL/TRACEBASE/tracebase3/.venv/lib/python3.11/site-packages/django/db/models/query.py", line 386, in __iter__
self._fetch_all()
File "/Users/rleach/PROJECT-LOCAL/TRACEBASE/tracebase3/.venv/lib/python3.11/site-packages/django/db/models/query.py", line 1954, in _fetch_all
self._result_cache = list(self._iterable_class(self))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/rleach/PROJECT-LOCAL/TRACEBASE/tracebase3/.venv/lib/python3.11/site-packages/django/db/models/query.py", line 234, in __iter__
return compiler.results_iter(
^^^^^^^^^^^^^^^^^^^^^^
File "/Users/rleach/PROJECT-LOCAL/TRACEBASE/tracebase3/.venv/lib/python3.11/site-packages/django/db/models/sql/compiler.py", line 1572, in results_iter
results = self.execute_sql(
^^^^^^^^^^^^^^^^^
File "/Users/rleach/PROJECT-LOCAL/TRACEBASE/tracebase3/.venv/lib/python3.11/site-packages/django/db/models/sql/compiler.py", line 1623, in execute_sql
cursor.execute(sql, params)
File "/Users/rleach/PROJECT-LOCAL/TRACEBASE/tracebase3/.venv/lib/python3.11/site-packages/django/db/backends/utils.py", line 79, in execute
return self._execute_with_wrappers(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/rleach/PROJECT-LOCAL/TRACEBASE/tracebase3/.venv/lib/python3.11/site-packages/django/db/backends/utils.py", line 92, in _execute_with_wrappers
return executor(sql, params, many, context)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/rleach/PROJECT-LOCAL/TRACEBASE/tracebase3/.venv/lib/python3.11/site-packages/django/db/backends/utils.py", line 100, in _execute
with self.db.wrap_database_errors:
File "/Users/rleach/PROJECT-LOCAL/TRACEBASE/tracebase3/.venv/lib/python3.11/site-packages/django/db/utils.py", line 91, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "/Users/rleach/PROJECT-LOCAL/TRACEBASE/tracebase3/.venv/lib/python3.11/site-packages/django/db/backends/utils.py", line 105, in _execute
return self.cursor.execute(sql, params)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
django.db.utils.ProgrammingError: SELECT DISTINCT ON expressions must match initial ORDER BY expressions
LINE 1: ...o_infusatetracer"."id", "DataRepo_tracer"."name", "DataRepo_...
^
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/Users/rleach/PROJECT-LOCAL/TRACEBASE/tracebase3/DataRepo/tests/views/search/formats/test_dataformat_group.py", line 612, in test_get_query_stats_full
res, _, _ = basv.perform_query(qry, "pgtemplate", generate_stats=True)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/rleach/PROJECT-LOCAL/TRACEBASE/tracebase3/DataRepo/views/search/formats/dataformat_group.py", line 587, in perform_query
data, based_on = self.get_query_stats(
^^^^^^^^^^^^^^^^^^^^^
File "/Users/rleach/PROJECT-LOCAL/TRACEBASE/tracebase3/DataRepo/views/search/formats/dataformat_group.py", line 805, in get_query_stats
raise UnsupportedDistinctCombo(all_distinct_fields) from pe
DataRepo.views.search.formats.dataformat_group.UnsupportedDistinctCombo: Unsupported combination of distinct fields: ['name', 'pk', 'labels__peak_group__name', 'labels__element', 'labels__pk', 'msrun_sample__sample__animal__infusate__name', 'msrun_sample__sample__animal__infusate__tracer_links__tracer__name', 'msrun_sample__sample__animal__infusate__tracer_links__concentration', 'msrun_sample__sample__animal__infusate__tracer_links__pk', 'msrun_sample__sample__animal__infusate__tracers__name', 'msrun_sample__sample__animal__infusate__tracers__pk', 'msrun_sample__sample__animal__infusate__tracers__compound__name', 'msrun_sample__sample__animal__infusate__tracers__compound__pk', 'compounds__name', 'compounds__pk', 'compounds__synonyms__compound__name', 'compounds__synonyms__name', 'compounds__synonyms__pk', 'msrun_sample__sample__animal__studies__name', 'msrun_sample__sample__animal__studies__pk', 'msrun_sample__sample__msrun_samples__ms_data_file__pk', 'msrun_sample__sample__msrun_samples__ms_raw_file__pk']. The problem likely stems from the usage of field references that are not real database fields, used in both .distinct() and .order_by(). Those methods resolve foreign keys (to database fields) in models differently. Be sure to supply actual database fields and not foreign key object references.
```
Let me see if there’s more that’s useful…