Django запрос с фильтром order by и distinct выдает ошибку
У меня есть такой запрос для получения объектов с фильтром и нашим отличием для пользователей, упорядоченных по дате
HallOfFame.objects.filter(group=group).order_by('-time').distinct("winner__id")
Выдается вот такая ошибка
Traceback (most recent call last):
File "D:\Programs\Python\Python38\lib\site-packages\django\db\backends\utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
psycopg2.errors.InvalidColumnReference: SELECT DISTINCT ON expressions must match initial ORDER BY expressions
LINE 1: SELECT DISTINCT ON ("groups_halloffame"."winner_id") "groups...
^
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "D:\Programs\Python\Python38\lib\site-packages\apscheduler\executors\base.py", line 125, in run_job
retval = job.func(*job.args, **job.kwargs)
File "E:\iku-django-backend\src\apps\groups\tasks.py", line 20, in hallOfFame
print(previous_hofs)
File "D:\Programs\Python\Python38\lib\site-packages\django\db\models\query.py", line 256, in __repr__
data = list(self[:REPR_OUTPUT_SIZE + 1])
File "D:\Programs\Python\Python38\lib\site-packages\django\db\models\query.py", line 262, in __len__
self._fetch_all()
File "D:\Programs\Python\Python38\lib\site-packages\django\db\models\query.py", line 1324, in _fetch_all
self._result_cache = list(self._iterable_class(self))
File "D:\Programs\Python\Python38\lib\site-packages\django\db\models\query.py", line 51, in __iter__
results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)
File "D:\Programs\Python\Python38\lib\site-packages\django\db\models\sql\compiler.py", line 1175, in execute_sql
cursor.execute(sql, params)
File "D:\Programs\Python\Python38\lib\site-packages\django\db\backends\utils.py", line 98, in execute
return super().execute(sql, params)
File "D:\Programs\Python\Python38\lib\site-packages\django\db\backends\utils.py", line 66, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File "D:\Programs\Python\Python38\lib\site-packages\django\db\backends\utils.py", line 75, in _execute_with_wrappers
return executor(sql, params, many, context)
File "D:\Programs\Python\Python38\lib\site-packages\django\db\backends\utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "D:\Programs\Python\Python38\lib\site-packages\django\db\utils.py", line 90, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "D:\Programs\Python\Python38\lib\site-packages\django\db\backends\utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: SELECT DISTINCT ON expressions must match initial ORDER BY expressions
LINE 1: SELECT DISTINCT ON ("groups_halloffame"."winner_id") "groups...
Нужно знать, что это проблема postgres
Моя модель HallOfFame выглядит следующим образом
class HallOfFame(models.Model):
group = models.ForeignKey(Community, on_delete=CASCADE)
winner = models.ForeignKey(
settings.AUTH_USER_MODEL, related_name="winner", on_delete=SET_NULL, null=True)
winner_score = models.BigIntegerField(null=True, blank=True)
time = models.DateTimeField(auto_now_add=True)