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.

feat(scoping): fail tests on lazy loads across peer rows (#126622)

Paul D'Ambra422ba0c9b3fd
diff --git a/.agents/skills/improving-drf-endpoints/SKILL.md b/.agents/skills/improving-drf-endpoints/SKILL.md
index 58c3cf8089cb..f4cb14b8b5d5 100644
--- a/.agents/skills/improving-drf-endpoints/SKILL.md
+++ b/.agents/skills/improving-drf-endpoints/SKILL.md
@@ -69,7 +69,7 @@ See [serializer-fields.md](references/serializer-fields.md) for patterns and exa
14. **Prefer `@validated_request`** over manual `serializer.is_valid()` + `@extend_schema` — it handles both in one decorator
15. **ViewSets outside `products/` need `@extend_schema(extensions={"x-product": "<product>"})`** — ViewSets in `products/<name>/backend/` are auto-attributed via module path; ViewSets in `posthog/api/` or `ee/` aren't and must declare attribution explicitly via the `x-product` extension. Accepts a plain string (`"product_analytics"`) or `ProductKey.X` enum (kebab values are normalized). Don't use `tags=["<product>"]` to influence codegen routing — `tags` is for Swagger UI display only. Without `x-product`, the MCP scaffold and frontend type generator can't route the endpoint to the right product
16. **`partial_update` `request=` override must be a superset of runtime write fields** — `extend_schema(request=CustomSerializer)` replaces drf-spectacular's inference from `serializer_class`; omitted fields disappear from OpenAPI, frontend types, and MCP tool schemas even when the runtime serializer still accepts them. After changing the override, run `hogli build:openapi` and verify generated MCP tool schemas still expose every OpenAPI body field
-17. **Wrap the list queryset with `strict_fetch()`** from `posthog.models.utils`, in `safely_get_queryset` when `self.action == "list"`. In tests and local dev, a lazy load of a forward relation or deferred field then raises `FieldFetchBlocked`, so a missing `select_related` fails the list test. In production, the missing field costs one extra query for the page, not one per row. Fix a failure with the `select_related` the error names, or stop deferring that field. The guard does not cover reverse FK or M2M managers, so keep `prefetch_related` and an `assertNumQueries` test for those. Reference: `AnnotationsViewSet` in `products/annotations/backend/api/annotation.py`
+17. **Wrap the list queryset with `strict_fetch()`** from `posthog.models.utils`, in `safely_get_queryset` when `self.action == "list"`. In tests and local dev, a lazy load of a forward relation or deferred field then raises `FieldFetchBlocked`, so a missing `select_related` fails the list test. In production, the missing field costs one extra query for the page, not one per row. Fix a failure with the `select_related` the error names, or stop deferring that field. The guard does not cover reverse FK or M2M managers, so keep `prefetch_related` and an `assertNumQueries` test for those. Reference: `AnnotationsViewSet` in `products/annotations/backend/api/annotation.py`. Team-scoped models raise in tests without `strict_fetch()` when a lazy load reads across rows of one query, unless the queryset sets `.fetch_mode(FETCH_RELATION_PEERS)`; see [the scoping README](../../../posthog/models/scoping/README.md#in-tests)
**Streaming endpoints:** For SSE or streaming responses, use `@extend_schema(request=InputSerializer, responses={(200, "text/event-stream"): OpenApiTypes.STR})` to document the request schema even though the response can't be fully typed.
diff --git a/conftest.py b/conftest.py
index 77b88127b8dd..9c79f7133cc3 100644
--- a/conftest.py
+++ b/conftest.py
@@ -262,20 +262,55 @@ def pytest_collection_finish() -> None:
temporal_test_servers.install()
-def pytest_runtest_setup(item: pytest.Item) -> None:
+def _fail_on_caught_peer_lazy_loads() -> None:
+ from posthog.models.scoping import peer_lazy_loads # noqa: PLC0415
+
+ # A blocked lazy load that app code caught still fails the test phase that made it.
+ if blocked := peer_lazy_loads.finish_test():
+ pytest.fail("\n".join(["Caught FieldFetchBlocked:", *blocked]))
+
+
[email protected](wrapper=True)
+def pytest_runtest_setup(item: pytest.Item) -> Generator[None]:
+ from posthog.models.scoping import peer_lazy_loads # noqa: PLC0415
from posthog.test import authentication_checks # noqa: PLC0415
marker = item.get_closest_marker("covers_authentication")
authentication_checks.start_test(marker.args if marker else ())
+ peer_lazy_loads.start_test()
+ try:
+ result = yield
+ except (pytest.skip.Exception, pytest.xfail.Exception):
+ # A skip or xfail must not hide a blocked load caught before it.
+ _fail_on_caught_peer_lazy_loads()
+ raise
+ _fail_on_caught_peer_lazy_loads()
+ return result
@pytest.hookimpl(wrapper=True)
def pytest_runtest_call(item: pytest.Item) -> Generator[None]:
from posthog.test import authentication_checks # noqa: PLC0415
- result = yield
+ try:
+ result = yield
+ except (pytest.skip.Exception, pytest.xfail.Exception):
+ _fail_on_caught_peer_lazy_loads()
+ raise
if problems := authentication_checks.finish_test():
pytest.fail("\n".join(problems))
+ _fail_on_caught_peer_lazy_loads()
+ return result
+
+
[email protected](wrapper=True)
+def pytest_runtest_teardown(item: pytest.Item, nextitem: pytest.Item | None) -> Generator[None]:
+ from posthog.models.scoping import peer_lazy_loads # noqa: PLC0415
+
+ # Drop what a failed call left, so teardown reports only its own loads.
+ peer_lazy_loads.start_test()
+ result = yield
+ _fail_on_caught_peer_lazy_loads()
return result
diff --git a/posthog/api/tagged_item.py b/posthog/api/tagged_item.py
index 458d63da0f02..654ed9a15b82 100644
--- a/posthog/api/tagged_item.py
+++ b/posthog/api/tagged_item.py
@@ -56,7 +56,12 @@ def _set_tags_on_object(tags: list[str], obj: Any) -> tuple[list[TaggedItem], se
# Delete tags that are missing (use individual deletes to trigger activity logging)
removed_tag_ids: set[int] = set()
- tagged_items_to_delete = obj.tagged_items.exclude(tag__name__in=deduped_tags)
+ # Activity logging reads each deleted item's tag, its team and the tagged object.
+ tagged_items_to_delete = (
+ obj.tagged_items.exclude(tag__name__in=deduped_tags)
+ .select_related("tag__team")
+ .prefetch_related("integer_object", "uuid_object")
+ )
for tagged_item in tagged_items_to_delete:
removed_tag_ids.add(tagged_item.tag_id)
tagged_item.delete()
diff --git a/posthog/models/scoping/README.md b/posthog/models/scoping/README.md
index 0855956a2ff6..be5e25c39d8e 100644
--- a/posthog/models/scoping/README.md
+++ b/posthog/models/scoping/README.md
@@ -71,6 +71,21 @@ Use `select_related()` in a streamed loop that reads a relation.
To opt out, call `.fetch_mode(models.FETCH_ONE)` on the queryset.
+### In tests
+
+Under pytest, scoped querysets use `FETCH_RAISE_ON_PEERS` instead.
+A lazy load on a row from a multi-row query raises `FieldFetchBlocked`, because without the batch it costs one query per row.
+The error names the model, the field, and the fix: `select_related()` or `prefetch_related()` for a relation, `only()` for a deferred field.
+A row from `.get()`, `.first()` or a one-row result still loads lazily.
+
+Django fetch modes cover forward foreign keys, one-to-one fields in both directions, generic foreign keys and deferred fields.
+They do not cover reverse foreign key or many-to-many managers such as `flag.experiments.all()`, so use `prefetch_related()` and an `assertNumQueries` test for those.
+
+If app code catches the `FieldFetchBlocked`, the root `conftest.py` still fails the test that made the load.
+
+Fix a failure with `select_related()`, `prefetch_related()` or `only()`.
+To keep production batching on purpose, set `.fetch_mode(FETCH_RELATION_PEERS)` on the queryset that loads the rows.
+
## Adoption
### New main-DB model
diff --git a/posthog/models/scoping/manager.py b/posthog/models/scoping/manager.py
index 0ebabf7ff301..cf40bf0713e1 100644
--- a/posthog/models/scoping/manager.py
+++ b/posthog/models/scoping/manager.py
@@ -16,14 +16,18 @@ raise on framework code that has no team context. Raw SQL bypasses the
ORM entirely. Use this alongside explicit team checks at the API layer.
"""
+import sys
from collections.abc import AsyncIterator, Iterator
+from functools import cache
from typing import Any, TypeVar, cast
+from django.conf import settings
+from django.core.exceptions import FieldFetchBlocked
from django.db import models
-from django.db.models.fetch_modes import FetchPeers
+from django.db.models.fetch_modes import FetchMode, FetchPeers
from django.db.models.query_utils import DeferredAttribute
-from posthog.models.scoping import get_current_team_id
+from posthog.models.scoping import get_current_team_id, peer_lazy_loads
T = TypeVar("T", bound=models.Model)
@@ -48,6 +52,61 @@ class FetchRelationPeers(FetchPeers):
FETCH_RELATION_PEERS = FetchRelationPeers()
+def _has_live_peers(instance: models.Model) -> bool:
+ live = 0
+ for peer in instance._state.peers:
+ if peer() is not None:
+ live += 1
+ if live > 1:
+ return True
+ return False
+
+
+def _fix_hint(fetcher: Any, field: str) -> str:
+ if isinstance(fetcher, DeferredAttribute):
+ return f'add "{field}" to only(), or remove it from defer()'
+ # Only a GenericForeignKey has a content type field, and select_related() cannot follow one.
+ # Importing GenericForeignKey here would load the ContentType model before the app registry is ready.
+ if hasattr(fetcher.field, "ct_field"):
+ return f'add prefetch_related("{field}")'
+ return f'add select_related("{field}") or prefetch_related("{field}")'
+
+
+class FetchRaiseOnPeers(FetchRelationPeers):
+ """Test-only `FETCH_RELATION_PEERS` that raises when a lazy load would run once per row.
+
+ A row from `.get()`, `.first()` or a one-row result has no live peers and loads as usual.
+ A queryset that sets `.fetch_mode(FETCH_RELATION_PEERS)` loads like production.
+ """
+
+ __slots__ = ()
+
+ def fetch(self, fetcher: Any, instance: models.Model) -> None:
+ label, field = instance._meta.label, fetcher.field.name
+ if _has_live_peers(instance):
+ message = (
+ f"Lazy load of {label}.{field} on a row from a multi-row query, which costs one query per row. "
+ f"Load it with the query: {_fix_hint(fetcher, field)}."
+ )
+ peer_lazy_loads.record(message)
+ raise FieldFetchBlocked(message) from None
+ super().fetch(fetcher, instance)
+
+ def __reduce__(self) -> str:
+ return "FETCH_RAISE_ON_PEERS"
+
+
+FETCH_RAISE_ON_PEERS = FetchRaiseOnPeers()
+
+
+@cache
+def default_fetch_mode() -> FetchMode:
+ # Test processes load pytest, unlike servers and management commands with TEST=1.
+ if settings.TEST and sys.modules.get("pytest") is not None:
+ return FETCH_RAISE_ON_PEERS
+ return FETCH_RELATION_PEERS
+
+
class TeamScopeError(Exception):
"""Raised when a team-scoped model is queried without team context.
@@ -90,12 +149,14 @@ class TeamScopedQuerySet(models.QuerySet[T]):
need to explicitly query across teams.
Defaults to `FETCH_RELATION_PEERS`: a lazy related read on one row loads
- it for every row the same query returned, in one query.
+ it for every row the same query returned, in one query. Tests use
+ `FETCH_RAISE_ON_PEERS`, so that read raises unless the queryset sets
+ `.fetch_mode(FETCH_RELATION_PEERS)`.
"""
def __init__(self, *args: Any, **kwargs: Any) -> None:
super().__init__(*args, **kwargs)
- self._fetch_mode = FETCH_RELATION_PEERS
+ self._fetch_mode = default_fetch_mode()
def iterator(self, chunk_size: int | None = None) -> Iterator[T]:
return super(TeamScopedQuerySet, self._without_streamed_peers()).iterator(chunk_size)
@@ -107,7 +168,7 @@ class TeamScopedQuerySet(models.QuerySet[T]):
# A streamed query shares one peer list across all its rows, and FETCH_PEERS
# scans that list on each lazy read, so a loop that reads a relation per row
# costs O(rows²). The rows a loop has dropped are not batched anyway.
- if self._fetch_mode is FETCH_RELATION_PEERS:
+ if isinstance(self._fetch_mode, FetchRelationPeers):
return self.fetch_mode(models.FETCH_ONE)
return self
diff --git a/posthog/models/scoping/peer_lazy_loads.py b/posthog/models/scoping/peer_lazy_loads.py
new file mode 100644
index 000000000000..cb566e3be8a7
--- /dev/null
+++ b/posthog/models/scoping/peer_lazy_loads.py
@@ -0,0 +1,22 @@
+"""Lazy loads across peer rows that `FETCH_RAISE_ON_PEERS` blocked during the current test.
+
+The fetch mode records each blocked load before it raises `FieldFetchBlocked`.
+The root conftest fails the test when a record is left at the end of its call,
+so a blocked load fails its test even when app code catches the exception.
+"""
+
+_blocked: list[str] = []
+
+
+def record(message: str) -> None:
+ _blocked.append(message)
+
+
+def start_test() -> None:
+ _blocked.clear()
+
+
+def finish_test() -> list[str]:
+ blocked = list(_blocked)
+ _blocked.clear()
+ return blocked
diff --git a/posthog/models/scoping/test_manager.py b/posthog/models/scoping/test_manager.py
index 8da8172430aa..63c1960f3097 100644
--- a/posthog/models/scoping/test_manager.py
+++ b/posthog/models/scoping/test_manager.py
@@ -1,23 +1,38 @@
+import sys
from collections.abc import Callable, Iterable
import pytest
from posthog.test.base import BaseTest
+from unittest.mock import patch
+
+from django.core.exceptions import FieldFetchBlocked
from parameterized import parameterized
-from posthog.models.scoping import team_scope, unscoped
-from posthog.models.scoping.manager import TeamScopedManager, TeamScopeError
+from posthog.models.scoping import peer_lazy_loads, team_scope, unscoped
+from posthog.models.scoping.manager import (
+ FETCH_RAISE_ON_PEERS,
+ FETCH_RELATION_PEERS,
+ TeamScopedManager,
+ TeamScopedQuerySet,
+ TeamScopeError,
+ default_fetch_mode,
+)
from posthog.models.team import Team
from products.feature_flags.backend.models.feature_flag import FeatureFlag
+def _flag_manager() -> TeamScopedManager[FeatureFlag]:
+ manager: TeamScopedManager[FeatureFlag] = TeamScopedManager()
+ manager.model = FeatureFlag
+ manager._db = "default"
+ return manager
+
+
class TestTeamScopedManager(BaseTest):
def _make_manager(self) -> TeamScopedManager[FeatureFlag]:
- manager: TeamScopedManager[FeatureFlag] = TeamScopedManager()
- manager.model = FeatureFlag
- manager._db = "default"
- return manager
+ return _flag_manager()
def test_no_scope_raises_team_scope_error(self):
with pytest.raises(TeamScopeError, match="No team context set"):
@@ -77,11 +92,36 @@ class TestTeamScopedManager(BaseTest):
@parameterized.expand(
[
- ("ambient_scope", lambda manager, team_id: manager.get_queryset().filter(active=True), 2),
- ("unscoped", lambda manager, team_id: manager.unscoped().filter(team_id=team_id), 2),
- ("for_team", lambda manager, team_id: manager.for_team(team_id, canonical=True), 2),
- ("queryset_unscoped", lambda manager, team_id: manager.unscoped().unscoped().filter(team_id=team_id), 2),
- ("iterator", lambda manager, team_id: manager.for_team(team_id, canonical=True).iterator(), 4),
+ (
+ "ambient_scope",
+ lambda manager, team_id: manager.get_queryset().filter(active=True).fetch_mode(FETCH_RELATION_PEERS),
+ 2,
+ ),
+ (
+ "unscoped",
+ lambda manager, team_id: manager.unscoped().filter(team_id=team_id).fetch_mode(FETCH_RELATION_PEERS),
+ 2,
+ ),
+ (
+ "for_team",
+ lambda manager, team_id: manager.for_team(team_id, canonical=True).fetch_mode(FETCH_RELATION_PEERS),
+ 2,
+ ),
+ (
+ "queryset_unscoped",
+ lambda manager, team_id: (
+ manager.unscoped().unscoped().filter(team_id=team_id).fetch_mode(FETCH_RELATION_PEERS)
+ ),
+ 2,
+ ),
+ (
+ "iterator",
+ lambda manager, team_id: (
+ manager.for_team(team_id, canonical=True).fetch_mode(FETCH_RELATION_PEERS).iterator()
+ ),
+ 4,
+ ),
+ ("iterator_test_default", lambda manager, team_id: manager.for_team(team_id, canonical=True).iterator(), 4),
]
)
def test_related_read_on_every_row_batches_across_peers(
@@ -103,7 +143,13 @@ class TestTeamScopedManager(BaseTest):
for key in ("flag-1", "flag-2"):
FeatureFlag.objects.create(team=self.team, key=key, name=key, created_by=self.user)
- first, second = list(self._make_manager().for_team(self.team.id, canonical=True).only("id").order_by("key"))
+ first, second = list(
+ self._make_manager()
+ .for_team(self.team.id, canonical=True)
+ .fetch_mode(FETCH_RELATION_PEERS)
+ .only("id")
+ .order_by("key")
+ )
first.name = "edited"
assert second.name == "flag-2"
@@ -113,7 +159,13 @@ class TestTeamScopedManager(BaseTest):
for key in ("flag-1", "flag-2"):
FeatureFlag.objects.create(team=self.team, key=key, name=key, created_by=self.user)
- first, second = list(self._make_manager().for_team(self.team.id, canonical=True).only("id").order_by("key"))
+ first, second = list(
+ self._make_manager()
+ .for_team(self.team.id, canonical=True)
+ .fetch_mode(FETCH_RELATION_PEERS)
+ .only("id")
+ .order_by("key")
+ )
FeatureFlag.objects.filter(pk=second.pk).delete()
assert first.name == "flag-1"
@@ -130,6 +182,80 @@ class TestTeamScopedManager(BaseTest):
assert get_current_team_id() == self.team.id
+class TestFetchRaiseOnPeers(BaseTest):
+ def setUp(self) -> None:
+ super().setUp()
+ for key in ("flag-0", "flag-1", "flag-2"):
+ FeatureFlag.objects.create(team=self.team, key=key, name=key, created_by=self.user)
+
+ def _flags(self) -> TeamScopedQuerySet[FeatureFlag]:
+ return _flag_manager().for_team(self.team.id, canonical=True).order_by("key")
+
+ @parameterized.expand(
+ [
+ ("relation", lambda qs: qs, lambda flag: flag.created_by, "FeatureFlag.created_by", "select_related"),
+ ("deferred_field", lambda qs: qs.only("id"), lambda flag: flag.name, "FeatureFlag.name", "only"),
+ ]
+ )
+ def test_lazy_load_across_peer_rows_raises(
+ self,
+ _name: str,
+ shape: Callable[[TeamScopedQuerySet[FeatureFlag]], TeamScopedQuerySet[FeatureFlag]],
+ read: Callable[[FeatureFlag], object],
+ model_field: str,
+ fix: str,
+ ) -> None:
+ first, *_ = list(shape(self._flags()))
+
+ with pytest.raises(FieldFetchBlocked, match=rf"{model_field}\b.*{fix}"):
+ read(first)
+
+ [blocked] = peer_lazy_loads.finish_test()
+ assert model_field in blocked
+
+ @parameterized.expand(
+ [
+ ("single_row_relation", lambda qs: [qs.get(key="flag-0")], lambda flag: flag.created_by, 1),
+ ("single_row_deferred_field", lambda qs: [qs.only("id").get(key="flag-0")], lambda flag: flag.name, 1),
+ ("select_related", lambda qs: list(qs.select_related("created_by")), lambda flag: flag.created_by, 0),
+ ("only_includes_field", lambda qs: list(qs.only("id", "name")), lambda flag: flag.name, 0),
+ ]
+ )
+ def test_read_that_costs_no_query_per_row_is_allowed(
+ self,
+ _name: str,
+ load: Callable[[TeamScopedQuerySet[FeatureFlag]], list[FeatureFlag]],
+ read: Callable[[FeatureFlag], object],
+ expected_queries: int,
+ ) -> None:
+ flags = load(self._flags())
+
+ with self.assertNumQueries(expected_queries):
+ for flag in flags:
+ read(flag)
+
+ def test_queryset_that_sets_the_production_mode_batches_across_peer_rows(self) -> None:
+ flags = list(self._flags().fetch_mode(FETCH_RELATION_PEERS))
+
+ with self.assertNumQueries(1):
+ for flag in flags:
+ assert flag.created_by == self.user
+
+ @parameterized.expand(
+ [
+ ("under_pytest", True, FETCH_RAISE_ON_PEERS),
+ ("outside_pytest", False, FETCH_RELATION_PEERS),
+ ]
+ )
+ def test_default_fetch_mode(self, _name: str, under_pytest: bool, expected: object) -> None:
+ default_fetch_mode.cache_clear()
+ try:
+ with patch.dict(sys.modules, {"pytest": pytest if under_pytest else None}):
+ assert default_fetch_mode() is expected
+ finally:
+ default_fetch_mode.cache_clear()
+
+
class TestTeamScopedQuerySet(BaseTest):
def test_queryset_unscoped_returns_fresh_queryset(self):
from posthog.models.scoping.manager import TeamScopedQuerySet
diff --git a/products/autoresearch/backend/temporal/workflows.py b/products/autoresearch/backend/temporal/workflows.py
index 6a26416ab00b..966711b4c78d 100644
--- a/products/autoresearch/backend/temporal/workflows.py
+++ b/products/autoresearch/backend/temporal/workflows.py
@@ -784,7 +784,8 @@ def activity_load_active_pipelines(inp: LoadActivePipelinesInput) -> LoadActiveP
# A null is_active counts as deactivated, as ActiveOrganizationPermission reads it.
.filter(team__organization__is_active=True)
.exclude(team__organization__is_pending_deletion=True)
- .select_related("team", "created_by")
+ # The access check reads each team's organization.
+ .select_related("team__organization", "created_by")
.order_by("created_at")
)
pipelines: list[tuple[bool, ActivePipeline]] = []
diff --git a/products/cdp/backend/api/test/test_hog_function_drafts.py b/products/cdp/backend/api/test/test_hog_function_drafts.py
index c97e1302e5bf..3326e7965e21 100644
--- a/products/cdp/backend/api/test/test_hog_function_drafts.py
+++ b/products/cdp/backend/api/test/test_hog_function_drafts.py
@@ -636,7 +636,7 @@ class TestHogFunctionRevisions(DraftTestCase):
self._live_edit(function_id, {"hog": EDITED_HOG})
- revisions = list(self._revisions(function_id).order_by("version"))
+ revisions = list(self._revisions(function_id).select_related("created_by").order_by("version"))
assert [revision.version for revision in revisions] == [1, 2]
# Rollback to the state before the revision system saw this function must be possible.
assert revisions[0].content["hog"] == LIVE_HOG
diff --git a/products/customer_analytics/backend/facade/api.py b/products/customer_analytics/backend/facade/api.py
index e920a1ce0dc3..5706ad38a45e 100644
--- a/products/customer_analytics/backend/facade/api.py
+++ b/products/customer_analytics/backend/facade/api.py
@@ -1016,7 +1016,13 @@ def _set_tags(
if created:
added_tags.append(tag_instance)
removed_tags: list[Tag] = []
- for tagged_item in account.tagged_items.exclude(tag__name__in=deduped_tags).select_related("tag"):
+ # Activity logging reads each deleted item's tag, its team and the tagged object.
+ removed_items = (
+ account.tagged_items.exclude(tag__name__in=deduped_tags)
+ .select_related("tag__team")
+ .prefetch_related("uuid_object")
+ )
+ for tagged_item in removed_items:
removed_tags.append(tagged_item.tag)
tagged_item.delete()
if removed_tags:
diff --git a/products/customer_analytics/backend/management/commands/test/test_seed_customer_analytics_accounts.py b/products/customer_analytics/backend/management/commands/test/test_seed_customer_analytics_accounts.py
index 5b790155c3bb..e388a6716ace 100644
--- a/products/customer_analytics/backend/management/commands/test/test_seed_customer_analytics_accounts.py
+++ b/products/customer_analytics/backend/management/commands/test/test_seed_customer_analytics_accounts.py
@@ -81,8 +81,10 @@ class TestSeedCustomerAnalyticsAccounts(BaseTest):
# Users: a pool joined to the org, assigned as account relationships.
assert len(self._pool_emails()) == 4
- holders = AccountRelationship.objects.for_team(self.team.pk).filter(
- account=accounts["acme-id"], ended_at__isnull=True
+ holders = (
+ AccountRelationship.objects.for_team(self.team.pk)
+ .filter(account=accounts["acme-id"], ended_at__isnull=True)
+ .select_related("definition", "user")
)
assert {rel.definition.name for rel in holders} == {"CSM", "Account executive", "Account owner"}
assert all(rel.user is not None and rel.user.email in self._pool_emails() for rel in holders)
diff --git a/products/customer_analytics/backend/test/test_external.py b/products/customer_analytics/backend/test/test_external.py
index eb64428d53a2..78e741bc1723 100644
--- a/products/customer_analytics/backend/test/test_external.py
+++ b/products/customer_analytics/backend/test/test_external.py
@@ -836,7 +836,11 @@ class TestExternalAccountCustomPropertiesAPI(APIBaseTest):
)
self.assertEqual(response.status_code, status.HTTP_200_OK)
- active = CustomPropertyValue.objects.for_team(self.team.id).filter(account=self.account, is_deleted=False)
+ active = (
+ CustomPropertyValue.objects.for_team(self.team.id)
+ .filter(account=self.account, is_deleted=False)
+ .select_related("definition")
+ )
self.assertEqual(
{(v.definition.name, v.value_str, v.value_num) for v in active},
{
diff --git a/products/data_catalog/backend/logic/lineage.py b/products/data_catalog/backend/logic/lineage.py
index 8d77f46027f3..8e5898535a45 100644
--- a/products/data_catalog/backend/logic/lineage.py
+++ b/products/data_catalog/backend/logic/lineage.py
@@ -9,6 +9,7 @@ import structlog
from posthog.hogql.database.database import Database
from posthog.exceptions_capture import capture_exception
+from posthog.models import Team
from posthog.models.scoping import team_scope
from products.data_modeling.backend.facade.api import delete_metric_node, mark_metric_node_degraded, sync_metric_to_dag
@@ -106,11 +107,11 @@ def sync_metric_lineage(metric: Metric, database: Database | None = None) -> Lin
return LineageSyncOutcome.DEGRADED
-def remove_metric_lineage(metric: Metric) -> None:
+def remove_metric_lineage(metric: Metric, team: Team) -> None:
"""Drop the metric's lineage node. The metric row is already gone, so a failure here only
strands a node the next backfill removes, and must not turn a successful delete into an error."""
try:
- delete_metric_node(metric.team, metric.id)
+ delete_metric_node(team, metric.id)
except Exception as error:
capture_exception(error)
logger.exception("Failed to remove metric lineage", metric_id=str(metric.id), team_id=metric.team_id)
diff --git a/products/data_catalog/backend/logic/metrics.py b/products/data_catalog/backend/logic/metrics.py
index f253efb9cbfa..c4ceec450122 100644
--- a/products/data_catalog/backend/logic/metrics.py
+++ b/products/data_catalog/backend/logic/metrics.py
@@ -509,7 +509,7 @@ def _apply_soft_delete(metric: Metric) -> None:
def soft_delete_metric(metric: Metric, user: Optional[User] = None, request: "Request | None" = None) -> None:
with team_scope(metric.team_id):
_apply_soft_delete(metric)
- remove_metric_lineage(metric)
+ remove_metric_lineage(metric, metric.team)
capture_metric_event(METRIC_DELETED_EVENT, metric, team=metric.team, user=user, request=request)
@@ -639,7 +639,7 @@ def bulk_soft_delete_metrics(
_capture_after_commit(METRIC_DELETED_EVENT, deleted, team=team, user=user, request=request)
for metric in deleted:
- remove_metric_lineage(metric)
+ remove_metric_lineage(metric, team)
return deleted, skipped
diff --git a/products/data_catalog/backend/presentation/views.py b/products/data_catalog/backend/presentation/views.py
index 6fbaaaac7bc5..43d766c2d468 100644
--- a/products/data_catalog/backend/presentation/views.py
+++ b/products/data_catalog/backend/presentation/views.py
@@ -133,7 +133,7 @@ class MetricViewSet(TeamAndOrgViewSetMixin, viewsets.ModelViewSet):
metrics = metrics.filter(status=status_filter)
if definition_kind := self.request.query_params.get("definition_kind"):
metrics = metrics.filter(definition__kind=definition_kind)
- return metrics
+ return metrics.select_related("created_by", "owner")
def dangerously_get_required_scopes(self, request: Request, view: APIView) -> list[str] | None:
if getattr(view, "action", None) not in ("create", "update", "partial_update"):
diff --git a/products/field_notes/backend/api.py b/products/field_notes/backend/api.py
index 031d655c51b4..ce92eed0ad9c 100644
--- a/products/field_notes/backend/api.py
+++ b/products/field_notes/backend/api.py
@@ -168,4 +168,4 @@ class FieldNoteViewSet(TeamAndOrgViewSetMixin, viewsets.ModelViewSet):
host = self.request.query_params.get("host")
if host:
queryset = queryset.filter(host=host)
- return queryset.order_by("-created_at")
+ return queryset.select_related("created_by").order_by("-created_at")
diff --git a/products/replay_vision/backend/tests/test_observation_media.py b/products/replay_vision/backend/tests/test_observation_media.py
index dd080b60c918..b01ae0f76d8d 100644
--- a/products/replay_vision/backend/tests/test_observation_media.py
+++ b/products/replay_vision/backend/tests/test_observation_media.py
@@ -203,7 +203,11 @@ class TestObservationMedia(BaseTest):
observation_id=self.observation.id,
)
- linked = ReplayObservationMedia.objects.for_team(self.team.id).filter(observation_id=self.observation.id)
+ linked = (
+ ReplayObservationMedia.objects.for_team(self.team.id)
+ .filter(observation_id=self.observation.id)
+ .select_related("asset", "small_asset")
+ )
assert sorted((m.kind, m.position, m.asset_id, m.small_asset_id) for m in linked) == sorted(
[(f.kind, f.position, f.media_asset_id, f.small_media_asset_id) for f in prepared.frames[:2]]
+ [(f.kind, f.position, f.media_asset_id, None) for f in prepared.frames[2:3]]
diff --git a/products/tasks/backend/logic/services/living_artifacts.py b/products/tasks/backend/logic/services/living_artifacts.py
index 17c4cc2a0f3b..f4125824dced 100644
--- a/products/tasks/backend/logic/services/living_artifacts.py
+++ b/products/tasks/backend/logic/services/living_artifacts.py
@@ -1271,6 +1271,7 @@ def _capture_chart_delivery_events(run: TaskRun, outcomes: list[tuple[TaskArtifa
def _pending_slack_file_queryset(run: TaskRun) -> QuerySet[TaskArtifact]:
return (
TaskArtifact.objects.for_team(run.team_id)
+ .select_related("created_by")
.filter(
task_id=run.task_id,
adapter=TaskArtifact.Adapter.SLACK_FILE,
diff --git a/products/web_analytics/backend/content_autopilot/generation.py b/products/web_analytics/backend/content_autopilot/generation.py
index 5d3599b79dd6..afab80d60e84 100644
--- a/products/web_analytics/backend/content_autopilot/generation.py
+++ b/products/web_analytics/backend/content_autopilot/generation.py
@@ -862,6 +862,15 @@ def _fail_unfinished_proposal(proposal: ContentAutopilotProposal | None, message
_fail_proposal(proposal, message)
+def _refresh_proposal(opportunity: ContentAutopilotOpportunity) -> None:
+ # Drafting may link a proposal after this row loaded. Reload the link together with the proposal,
+ # so the read that follows is not a lazy load per row of the batch.
+ opportunity.refresh_from_db(
+ fields=["proposal"],
+ from_queryset=ContentAutopilotOpportunity.objects.for_team(opportunity.team_id).select_related("proposal"),
+ )
+
+
def _draft_opportunity(
client: Anthropic,
*,
@@ -883,7 +892,7 @@ def _draft_opportunity(
}
)
except SoftTimeLimitExceeded:
- opportunity.refresh_from_db(fields=["proposal"])
+ _refresh_proposal(opportunity)
_fail_unfinished_proposal(opportunity.proposal, TIMED_OUT_PROPOSAL_MESSAGE)
raise
except ContentAutopilotLLMError as error:
@@ -893,7 +902,7 @@ def _draft_opportunity(
capture_exception(error)
logger.exception("content_autopilot_generation_failed", team_id=run.team_id, run_id=str(run.id))
errors.append({"error_code": "draft_failed", "message": f"{title}: something went wrong while drafting."})
- opportunity.refresh_from_db(fields=["proposal"])
+ _refresh_proposal(opportunity)
_fail_proposal(opportunity.proposal, "Something went wrong while drafting. Regenerate to try again.")
finally:
if opportunity.proposal_id:
@@ -911,6 +920,7 @@ def generate_run(team_id: int, run_id: str, *, client: Anthropic | None = None)
try:
opportunities = list(
ContentAutopilotOpportunity.objects.for_team(team_id)
+ .select_related("proposal")
.filter(run=run, status=ContentAutopilotOpportunity.Status.QUEUED)
.order_by("-score")
)