Skip to content

fix(feature-flags): register evaluation context models#61051

Merged
andehen merged 3 commits into
masterfrom
fix/feature-flag-evaluation-context-registration
Jun 2, 2026
Merged

fix(feature-flags): register evaluation context models#61051
andehen merged 3 commits into
masterfrom
fix/feature-flag-evaluation-context-registration

Conversation

@andehen
Copy link
Copy Markdown
Contributor

@andehen andehen commented Jun 2, 2026

Problem

get_feature_flags() annotates on the flag_evaluation_contexts reverse relation when rebuilding the flag cache. Django only registers models that an app's models package imports at app-load time, but models/__init__.py here only re-exported TeamFeatureFlagDefaultsConfig. So FeatureFlagEvaluationContext was registered only when some other path happened to import it first. In processes where that hadn't happened, saving a flag (e.g. during experiment launch) triggered the cache rebuild and raised FieldError: Cannot resolve keyword 'flag_evaluation_contexts'.

Error: https://us.posthog.com/error_tracking/019e8616-5f0c-7560-9634-78a259a27f6e

Changes

Re-export the full feature-flags model graph from models/__init__.py so every model is registered during app loading, rather than incidentally when another module happens to import it.

How did you test this code?

Agent-assisted. Tested manually: confirmed via a fresh django.setup() that the flag_evaluation_contexts reverse relation resolves and the get_feature_flags() annotation that was raising FieldError now compiles. Existing evaluation-context cache tests still pass.

🤖 Agent context

Claude Code (Opus 4.8) investigated the root cause and reviewed the fix. An initial narrower approach — a side-effect import in feature_flag.py — was rejected in favor of registering the full model graph in models/__init__.py, the idiomatic Django fix that closes the gap for every model in the app, not just one path. Verified there's no app-load circular import: the core posthog app loads before product apps, and the feature-flags models only depend on posthog.*/django.*.

Django only registers models imported by an installed app's models package during app loading. The feature flags model package only imported TeamFeatureFlagDefaultsConfig, so FeatureFlagEvaluationContext could remain absent from the app registry until another request path imported it incidentally.

The flag cache rebuild annotates on the flag_evaluation_contexts reverse relation. If that relation has not been registered, Django raises FieldError while rebuilding the cache after a feature flag save, including during experiment launch.

Import the full feature flags model graph from models/__init__.py and add a regression test that verifies FeatureFlag can resolve the flag_evaluation_contexts reverse relation.
@greptile-apps
Copy link
Copy Markdown
Contributor

greptile-apps Bot commented Jun 2, 2026

Reviews (1): Last reviewed commit: "fix(feature-flags): register evaluation ..." | Re-trigger Greptile

andehen added 2 commits June 2, 2026 08:43
django-stubs's get_field plugin validates the string literal against
statically-known fields and cannot see the flag_evaluation_contexts reverse
relation (declared via related_name on FeatureFlagEvaluationContext), so it
failed with [misc]. Assert the relation through _meta.related_objects
instead, which avoids the plugin hook and matches the query name the cache
annotation actually uses.
The test imported FeatureFlagEvaluationContext itself, so it could never
observe the actual failure mode (the model absent from the app registry at
django.setup() because nothing imported it). The runtime annotate path
stays covered by the evaluation-context cache tests.
@andehen andehen added the stamphog Request AI review from stamphog label Jun 2, 2026
Copy link
Copy Markdown
Contributor

@github-actions github-actions Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trivial fix that registers existing model classes in the package's init.py — no behavioral changes, no new code, no security or data model concerns.

@github-project-automation github-project-automation Bot moved this from In Review to Approved in Feature Flags Jun 2, 2026
@andehen andehen enabled auto-merge (squash) June 2, 2026 09:08
@andehen andehen merged commit 8762879 into master Jun 2, 2026
333 of 349 checks passed
@andehen andehen deleted the fix/feature-flag-evaluation-context-registration branch June 2, 2026 09:51
@github-project-automation github-project-automation Bot moved this from Approved to Done in Feature Flags Jun 2, 2026
@deployment-status-posthog
Copy link
Copy Markdown

deployment-status-posthog Bot commented Jun 2, 2026

Deploy status

Environment Status Deployed At Workflow
dev ✅ Deployed 2026-06-02 10:16 UTC Run
prod-us ✅ Deployed 2026-06-02 10:34 UTC Run
prod-eu ✅ Deployed 2026-06-02 10:39 UTC Run

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

stamphog Request AI review from stamphog

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

1 participant