Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(media): tie-break media library pagination on id (#102402)

posthog[bot]61e75b01166e
diff --git a/posthog/api/test/test_uploaded_media.py b/posthog/api/test/test_uploaded_media.py
index 8a099f70e90a..39adc61acba9 100644
--- a/posthog/api/test/test_uploaded_media.py
+++ b/posthog/api/test/test_uploaded_media.py
@@ -3,12 +3,14 @@ import os
import re
import shutil
import tempfile
+from uuid import UUID
from posthog.test.base import APIBaseTest
from unittest.mock import patch
from django.core.files.uploadedfile import SimpleUploadedFile
from django.test import override_settings
+from django.utils.timezone import now
from boto3 import resource
from botocore.config import Config
@@ -389,6 +391,26 @@ class TestMediaLibraryAPI(APIBaseTest):
assert listed["url"] == f"http://localhost:8010/uploaded_media/{second.id}"
assert listed["created_at"] is not None
+ def test_list_pages_stay_stable_when_images_share_a_created_at(self) -> None:
+ # Ordering on a non-unique created_at alone leaves tied rows no fixed position, so a
+ # client walking offset pages can miss an image or receive one twice. The explicit ids
+ # are ascending, against the newest-first order, so insertion order cannot satisfy it.
+ ids = [UUID(f"0192f0f0-0000-7000-8000-00000000000{n}") for n in range(1, 5)]
+ for media_id in ids:
+ self._create_media(purpose="canvas", id=media_id)
+ # auto_now_add discards a created_at passed to create(), so force the tie afterwards.
+ UploadedMedia.objects.filter(id__in=ids).update(created_at=now())
+
+ paged_ids: list[str] = []
+ for offset in (0, 2):
+ response = self.client.get(
+ f"/api/projects/{self.team.id}/uploaded_media/?purpose=canvas&limit=2&offset={offset}"
+ )
+ self.assertEqual(response.status_code, status.HTTP_200_OK, response.json())
+ paged_ids.extend(item["id"] for item in response.json()["results"])
+
+ assert paged_ids == [str(media_id) for media_id in sorted(ids, reverse=True)]
+
@parameterized.expand(
[
("with_purpose", {"purpose": "email"}, 1),
diff --git a/posthog/api/uploaded_media.py b/posthog/api/uploaded_media.py
index 57a4de5e5998..580a41e2b43f 100644
--- a/posthog/api/uploaded_media.py
+++ b/posthog/api/uploaded_media.py
@@ -301,7 +301,7 @@ class MediaViewSet(TeamAndOrgViewSetMixin, viewsets.GenericViewSet):
def safely_get_queryset(self, queryset):
if self.action == "complete_upload":
return queryset.filter(pending=True)
- return queryset.filter(pending=False).order_by("-created_at")
+ return queryset.filter(pending=False).order_by("-created_at", "-id")
@extend_schema(
description="List images in the media library. Requires a `purpose` filter — the library is scoped per "