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(projects): let TaggedItem point at a project (#93351)

Yasend3c916186b25
diff --git a/posthog/api/test/dashboards/__snapshots__/test_dashboard.ambr b/posthog/api/test/dashboards/__snapshots__/test_dashboard.ambr
index 6b8a802ff61b..350d3c718c94 100644
--- a/posthog/api/test/dashboards/__snapshots__/test_dashboard.ambr
+++ b/posthog/api/test/dashboards/__snapshots__/test_dashboard.ambr
@@ -1596,6 +1596,7 @@
"posthog_taggeditem"."account_id",
"posthog_taggeditem"."endpoint_id",
"posthog_taggeditem"."replay_scanner_id",
+ "posthog_taggeditem"."project_id",
"posthog_tag"."id",
"posthog_tag"."name",
"posthog_tag"."team_id"
@@ -2282,6 +2283,7 @@
"posthog_taggeditem"."account_id",
"posthog_taggeditem"."endpoint_id",
"posthog_taggeditem"."replay_scanner_id",
+ "posthog_taggeditem"."project_id",
"posthog_tag"."id",
"posthog_tag"."name",
"posthog_tag"."team_id"
@@ -3744,6 +3746,7 @@
"posthog_taggeditem"."account_id",
"posthog_taggeditem"."endpoint_id",
"posthog_taggeditem"."replay_scanner_id",
+ "posthog_taggeditem"."project_id",
"posthog_tag"."id",
"posthog_tag"."name",
"posthog_tag"."team_id"
@@ -6408,6 +6411,7 @@
"posthog_taggeditem"."account_id",
"posthog_taggeditem"."endpoint_id",
"posthog_taggeditem"."replay_scanner_id",
+ "posthog_taggeditem"."project_id",
"posthog_tag"."id",
"posthog_tag"."name",
"posthog_tag"."team_id"
@@ -7087,6 +7091,7 @@
"posthog_taggeditem"."account_id",
"posthog_taggeditem"."endpoint_id",
"posthog_taggeditem"."replay_scanner_id",
+ "posthog_taggeditem"."project_id",
"posthog_tag"."id",
"posthog_tag"."name",
"posthog_tag"."team_id"
@@ -11752,6 +11757,7 @@
"posthog_taggeditem"."account_id",
"posthog_taggeditem"."endpoint_id",
"posthog_taggeditem"."replay_scanner_id",
+ "posthog_taggeditem"."project_id",
"posthog_tag"."id",
"posthog_tag"."name",
"posthog_tag"."team_id"
@@ -15188,6 +15194,7 @@
"posthog_taggeditem"."account_id",
"posthog_taggeditem"."endpoint_id",
"posthog_taggeditem"."replay_scanner_id",
+ "posthog_taggeditem"."project_id",
"posthog_tag"."id",
"posthog_tag"."name",
"posthog_tag"."team_id"
diff --git a/posthog/migrations/1335_taggeditem_project.py b/posthog/migrations/1335_taggeditem_project.py
new file mode 100644
index 000000000000..ed5600faf16f
--- /dev/null
+++ b/posthog/migrations/1335_taggeditem_project.py
@@ -0,0 +1,299 @@
+import django.db.models.deletion
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+ dependencies = [
+ ("posthog", "1334_remove_cimd_blocklist_entry"),
+ ]
+
+ operations = [
+ migrations.SeparateDatabaseAndState(
+ state_operations=[
+ migrations.RemoveConstraint(
+ model_name="taggeditem",
+ name="exactly_one_related_object",
+ ),
+ migrations.AlterUniqueTogether(
+ name="taggeditem",
+ unique_together=set(),
+ ),
+ migrations.AddField(
+ model_name="taggeditem",
+ name="project",
+ field=models.ForeignKey(
+ blank=True,
+ db_constraint=False,
+ null=True,
+ on_delete=django.db.models.deletion.CASCADE,
+ related_name="tagged_items",
+ to="posthog.project",
+ ),
+ ),
+ migrations.AlterUniqueTogether(
+ name="taggeditem",
+ unique_together={
+ (
+ "tag",
+ "dashboard",
+ "insight",
+ "event_definition",
+ "property_definition",
+ "action",
+ "feature_flag",
+ "experiment_saved_metric",
+ "ticket",
+ "account",
+ "endpoint",
+ "replay_scanner",
+ "project",
+ )
+ },
+ ),
+ migrations.AddConstraint(
+ model_name="taggeditem",
+ constraint=models.UniqueConstraint(
+ condition=models.Q(("project__isnull", False)),
+ fields=("tag", "project"),
+ name="unique_project_tagged_item",
+ ),
+ ),
+ migrations.AddConstraint(
+ model_name="taggeditem",
+ constraint=models.CheckConstraint(
+ condition=models.Q(
+ models.Q(
+ ("dashboard__isnull", False),
+ ("insight__isnull", True),
+ ("event_definition__isnull", True),
+ ("property_definition__isnull", True),
+ ("action__isnull", True),
+ ("feature_flag__isnull", True),
+ ("experiment_saved_metric__isnull", True),
+ ("ticket__isnull", True),
+ ("account__isnull", True),
+ ("endpoint__isnull", True),
+ ("replay_scanner__isnull", True),
+ ("project__isnull", True),
+ ),
+ models.Q(
+ ("dashboard__isnull", True),
+ ("insight__isnull", False),
+ ("event_definition__isnull", True),
+ ("property_definition__isnull", True),
+ ("action__isnull", True),
+ ("feature_flag__isnull", True),
+ ("experiment_saved_metric__isnull", True),
+ ("ticket__isnull", True),
+ ("account__isnull", True),
+ ("endpoint__isnull", True),
+ ("replay_scanner__isnull", True),
+ ("project__isnull", True),
+ ),
+ models.Q(
+ ("dashboard__isnull", True),
+ ("insight__isnull", True),
+ ("event_definition__isnull", False),
+ ("property_definition__isnull", True),
+ ("action__isnull", True),
+ ("feature_flag__isnull", True),
+ ("experiment_saved_metric__isnull", True),
+ ("ticket__isnull", True),
+ ("account__isnull", True),
+ ("endpoint__isnull", True),
+ ("replay_scanner__isnull", True),
+ ("project__isnull", True),
+ ),
+ models.Q(
+ ("dashboard__isnull", True),
+ ("insight__isnull", True),
+ ("event_definition__isnull", True),
+ ("property_definition__isnull", False),
+ ("action__isnull", True),
+ ("feature_flag__isnull", True),
+ ("experiment_saved_metric__isnull", True),
+ ("ticket__isnull", True),
+ ("account__isnull", True),
+ ("endpoint__isnull", True),
+ ("replay_scanner__isnull", True),
+ ("project__isnull", True),
+ ),
+ models.Q(
+ ("dashboard__isnull", True),
+ ("insight__isnull", True),
+ ("event_definition__isnull", True),
+ ("property_definition__isnull", True),
+ ("action__isnull", False),
+ ("feature_flag__isnull", True),
+ ("experiment_saved_metric__isnull", True),
+ ("ticket__isnull", True),
+ ("account__isnull", True),
+ ("endpoint__isnull", True),
+ ("replay_scanner__isnull", True),
+ ("project__isnull", True),
+ ),
+ models.Q(
+ ("dashboard__isnull", True),
+ ("insight__isnull", True),
+ ("event_definition__isnull", True),
+ ("property_definition__isnull", True),
+ ("action__isnull", True),
+ ("feature_flag__isnull", False),
+ ("experiment_saved_metric__isnull", True),
+ ("ticket__isnull", True),
+ ("account__isnull", True),
+ ("endpoint__isnull", True),
+ ("replay_scanner__isnull", True),
+ ("project__isnull", True),
+ ),
+ models.Q(
+ ("dashboard__isnull", True),
+ ("insight__isnull", True),
+ ("event_definition__isnull", True),
+ ("property_definition__isnull", True),
+ ("action__isnull", True),
+ ("feature_flag__isnull", True),
+ ("experiment_saved_metric__isnull", False),
+ ("ticket__isnull", True),
+ ("account__isnull", True),
+ ("endpoint__isnull", True),
+ ("replay_scanner__isnull", True),
+ ("project__isnull", True),
+ ),
+ models.Q(
+ ("dashboard__isnull", True),
+ ("insight__isnull", True),
+ ("event_definition__isnull", True),
+ ("property_definition__isnull", True),
+ ("action__isnull", True),
+ ("feature_flag__isnull", True),
+ ("experiment_saved_metric__isnull", True),
+ ("ticket__isnull", False),
+ ("account__isnull", True),
+ ("endpoint__isnull", True),
+ ("replay_scanner__isnull", True),
+ ("project__isnull", True),
+ ),
+ models.Q(
+ ("dashboard__isnull", True),
+ ("insight__isnull", True),
+ ("event_definition__isnull", True),
+ ("property_definition__isnull", True),
+ ("action__isnull", True),
+ ("feature_flag__isnull", True),
+ ("experiment_saved_metric__isnull", True),
+ ("ticket__isnull", True),
+ ("account__isnull", False),
+ ("endpoint__isnull", True),
+ ("replay_scanner__isnull", True),
+ ("project__isnull", True),
+ ),
+ models.Q(
+ ("dashboard__isnull", True),
+ ("insight__isnull", True),
+ ("event_definition__isnull", True),
+ ("property_definition__isnull", True),
+ ("action__isnull", True),
+ ("feature_flag__isnull", True),
+ ("experiment_saved_metric__isnull", True),
+ ("ticket__isnull", True),
+ ("account__isnull", True),
+ ("endpoint__isnull", False),
+ ("replay_scanner__isnull", True),
+ ("project__isnull", True),
+ ),
+ models.Q(
+ ("dashboard__isnull", True),
+ ("insight__isnull", True),
+ ("event_definition__isnull", True),
+ ("property_definition__isnull", True),
+ ("action__isnull", True),
+ ("feature_flag__isnull", True),
+ ("experiment_saved_metric__isnull", True),
+ ("ticket__isnull", True),
+ ("account__isnull", True),
+ ("endpoint__isnull", True),
+ ("replay_scanner__isnull", False),
+ ("project__isnull", True),
+ ),
+ models.Q(
+ ("dashboard__isnull", True),
+ ("insight__isnull", True),
+ ("event_definition__isnull", True),
+ ("property_definition__isnull", True),
+ ("action__isnull", True),
+ ("feature_flag__isnull", True),
+ ("experiment_saved_metric__isnull", True),
+ ("ticket__isnull", True),
+ ("account__isnull", True),
+ ("endpoint__isnull", True),
+ ("replay_scanner__isnull", True),
+ ("project__isnull", False),
+ ),
+ _connector="OR",
+ ),
+ name="exactly_one_related_object",
+ ),
+ ),
+ ],
+ database_operations=[
+ # posthog_project is read on nearly every request, so the column lands without
+ # an inline FK constraint. 1338 adds the constraint NOT VALID and 1339 validates
+ # it, which keeps the lock on the parent to a brief metadata-only ALTER.
+ migrations.RunSQL(
+ sql="""
+ ALTER TABLE "posthog_taggeditem" ADD COLUMN "project_id" bigint NULL;
+ """,
+ reverse_sql="""
+ ALTER TABLE "posthog_taggeditem" DROP COLUMN IF EXISTS "project_id";
+ """,
+ ),
+ migrations.RunSQL(
+ sql="""
+ ALTER TABLE "posthog_taggeditem" DROP CONSTRAINT IF EXISTS "exactly_one_related_object";
+ ALTER TABLE "posthog_taggeditem" ADD CONSTRAINT "exactly_one_related_object" CHECK ( /* -- existing-table-constraint-ignore */
+ (
+ (dashboard_id IS NOT NULL AND insight_id IS NULL AND event_definition_id IS NULL AND property_definition_id IS NULL AND action_id IS NULL AND feature_flag_id IS NULL AND experiment_saved_metric_id IS NULL AND ticket_id IS NULL AND account_id IS NULL AND endpoint_id IS NULL AND replay_scanner_id IS NULL AND project_id IS NULL) OR /* -- not-null-ignore */
+ (dashboard_id IS NULL AND insight_id IS NOT NULL AND event_definition_id IS NULL AND property_definition_id IS NULL AND action_id IS NULL AND feature_flag_id IS NULL AND experiment_saved_metric_id IS NULL AND ticket_id IS NULL AND account_id IS NULL AND endpoint_id IS NULL AND replay_scanner_id IS NULL AND project_id IS NULL) OR /* -- not-null-ignore */
+ (dashboard_id IS NULL AND insight_id IS NULL AND event_definition_id IS NOT NULL AND property_definition_id IS NULL AND action_id IS NULL AND feature_flag_id IS NULL AND experiment_saved_metric_id IS NULL AND ticket_id IS NULL AND account_id IS NULL AND endpoint_id IS NULL AND replay_scanner_id IS NULL AND project_id IS NULL) OR /* -- not-null-ignore */
+ (dashboard_id IS NULL AND insight_id IS NULL AND event_definition_id IS NULL AND property_definition_id IS NOT NULL AND action_id IS NULL AND feature_flag_id IS NULL AND experiment_saved_metric_id IS NULL AND ticket_id IS NULL AND account_id IS NULL AND endpoint_id IS NULL AND replay_scanner_id IS NULL AND project_id IS NULL) OR /* -- not-null-ignore */
+ (dashboard_id IS NULL AND insight_id IS NULL AND event_definition_id IS NULL AND property_definition_id IS NULL AND action_id IS NOT NULL AND feature_flag_id IS NULL AND experiment_saved_metric_id IS NULL AND ticket_id IS NULL AND account_id IS NULL AND endpoint_id IS NULL AND replay_scanner_id IS NULL AND project_id IS NULL) OR /* -- not-null-ignore */
+ (dashboard_id IS NULL AND insight_id IS NULL AND event_definition_id IS NULL AND property_definition_id IS NULL AND action_id IS NULL AND feature_flag_id IS NOT NULL AND experiment_saved_metric_id IS NULL AND ticket_id IS NULL AND account_id IS NULL AND endpoint_id IS NULL AND replay_scanner_id IS NULL AND project_id IS NULL) OR /* -- not-null-ignore */
+ (dashboard_id IS NULL AND insight_id IS NULL AND event_definition_id IS NULL AND property_definition_id IS NULL AND action_id IS NULL AND feature_flag_id IS NULL AND experiment_saved_metric_id IS NOT NULL AND ticket_id IS NULL AND account_id IS NULL AND endpoint_id IS NULL AND replay_scanner_id IS NULL AND project_id IS NULL) OR /* -- not-null-ignore */
+ (dashboard_id IS NULL AND insight_id IS NULL AND event_definition_id IS NULL AND property_definition_id IS NULL AND action_id IS NULL AND feature_flag_id IS NULL AND experiment_saved_metric_id IS NULL AND ticket_id IS NOT NULL AND account_id IS NULL AND endpoint_id IS NULL AND replay_scanner_id IS NULL AND project_id IS NULL) OR /* -- not-null-ignore */
+ (dashboard_id IS NULL AND insight_id IS NULL AND event_definition_id IS NULL AND property_definition_id IS NULL AND action_id IS NULL AND feature_flag_id IS NULL AND experiment_saved_metric_id IS NULL AND ticket_id IS NULL AND account_id IS NOT NULL AND endpoint_id IS NULL AND replay_scanner_id IS NULL AND project_id IS NULL) OR /* -- not-null-ignore */
+ (dashboard_id IS NULL AND insight_id IS NULL AND event_definition_id IS NULL AND property_definition_id IS NULL AND action_id IS NULL AND feature_flag_id IS NULL AND experiment_saved_metric_id IS NULL AND ticket_id IS NULL AND account_id IS NULL AND endpoint_id IS NOT NULL AND replay_scanner_id IS NULL AND project_id IS NULL) OR /* -- not-null-ignore */
+ (dashboard_id IS NULL AND insight_id IS NULL AND event_definition_id IS NULL AND property_definition_id IS NULL AND action_id IS NULL AND feature_flag_id IS NULL AND experiment_saved_metric_id IS NULL AND ticket_id IS NULL AND account_id IS NULL AND endpoint_id IS NULL AND replay_scanner_id IS NOT NULL AND project_id IS NULL) OR /* -- not-null-ignore */
+ (dashboard_id IS NULL AND insight_id IS NULL AND event_definition_id IS NULL AND property_definition_id IS NULL AND action_id IS NULL AND feature_flag_id IS NULL AND experiment_saved_metric_id IS NULL AND ticket_id IS NULL AND account_id IS NULL AND endpoint_id IS NULL AND replay_scanner_id IS NULL AND project_id IS NOT NULL) /* -- not-null-ignore */
+ )
+ ) NOT VALID;
+ """,
+ reverse_sql="""
+ ALTER TABLE "posthog_taggeditem" DROP CONSTRAINT IF EXISTS "exactly_one_related_object";
+ ALTER TABLE "posthog_taggeditem" ADD CONSTRAINT "exactly_one_related_object" CHECK ( /* -- existing-table-constraint-ignore */
+ (
+ (dashboard_id IS NOT NULL AND insight_id IS NULL AND event_definition_id IS NULL AND property_definition_id IS NULL AND action_id IS NULL AND feature_flag_id IS NULL AND experiment_saved_metric_id IS NULL AND ticket_id IS NULL AND account_id IS NULL AND endpoint_id IS NULL AND replay_scanner_id IS NULL) OR /* -- not-null-ignore */
+ (dashboard_id IS NULL AND insight_id IS NOT NULL AND event_definition_id IS NULL AND property_definition_id IS NULL AND action_id IS NULL AND feature_flag_id IS NULL AND experiment_saved_metric_id IS NULL AND ticket_id IS NULL AND account_id IS NULL AND endpoint_id IS NULL AND replay_scanner_id IS NULL) OR /* -- not-null-ignore */
+ (dashboard_id IS NULL AND insight_id IS NULL AND event_definition_id IS NOT NULL AND property_definition_id IS NULL AND action_id IS NULL AND feature_flag_id IS NULL AND experiment_saved_metric_id IS NULL AND ticket_id IS NULL AND account_id IS NULL AND endpoint_id IS NULL AND replay_scanner_id IS NULL) OR /* -- not-null-ignore */
+ (dashboard_id IS NULL AND insight_id IS NULL AND event_definition_id IS NULL AND property_definition_id IS NOT NULL AND action_id IS NULL AND feature_flag_id IS NULL AND experiment_saved_metric_id IS NULL AND ticket_id IS NULL AND account_id IS NULL AND endpoint_id IS NULL AND replay_scanner_id IS NULL) OR /* -- not-null-ignore */
+ (dashboard_id IS NULL AND insight_id IS NULL AND event_definition_id IS NULL AND property_definition_id IS NULL AND action_id IS NOT NULL AND feature_flag_id IS NULL AND experiment_saved_metric_id IS NULL AND ticket_id IS NULL AND account_id IS NULL AND endpoint_id IS NULL AND replay_scanner_id IS NULL) OR /* -- not-null-ignore */
+ (dashboard_id IS NULL AND insight_id IS NULL AND event_definition_id IS NULL AND property_definition_id IS NULL AND action_id IS NULL AND feature_flag_id IS NOT NULL AND experiment_saved_metric_id IS NULL AND ticket_id IS NULL AND account_id IS NULL AND endpoint_id IS NULL AND replay_scanner_id IS NULL) OR /* -- not-null-ignore */
+ (dashboard_id IS NULL AND insight_id IS NULL AND event_definition_id IS NULL AND property_definition_id IS NULL AND action_id IS NULL AND feature_flag_id IS NULL AND experiment_saved_metric_id IS NOT NULL AND ticket_id IS NULL AND account_id IS NULL AND endpoint_id IS NULL AND replay_scanner_id IS NULL) OR /* -- not-null-ignore */
+ (dashboard_id IS NULL AND insight_id IS NULL AND event_definition_id IS NULL AND property_definition_id IS NULL AND action_id IS NULL AND feature_flag_id IS NULL AND experiment_saved_metric_id IS NULL AND ticket_id IS NOT NULL AND account_id IS NULL AND endpoint_id IS NULL AND replay_scanner_id IS NULL) OR /* -- not-null-ignore */
+ (dashboard_id IS NULL AND insight_id IS NULL AND event_definition_id IS NULL AND property_definition_id IS NULL AND action_id IS NULL AND feature_flag_id IS NULL AND experiment_saved_metric_id IS NULL AND ticket_id IS NULL AND account_id IS NOT NULL AND endpoint_id IS NULL AND replay_scanner_id IS NULL) OR /* -- not-null-ignore */
+ (dashboard_id IS NULL AND insight_id IS NULL AND event_definition_id IS NULL AND property_definition_id IS NULL AND action_id IS NULL AND feature_flag_id IS NULL AND experiment_saved_metric_id IS NULL AND ticket_id IS NULL AND account_id IS NULL AND endpoint_id IS NOT NULL AND replay_scanner_id IS NULL) OR /* -- not-null-ignore */
+ (dashboard_id IS NULL AND insight_id IS NULL AND event_definition_id IS NULL AND property_definition_id IS NULL AND action_id IS NULL AND feature_flag_id IS NULL AND experiment_saved_metric_id IS NULL AND ticket_id IS NULL AND account_id IS NULL AND endpoint_id IS NULL AND replay_scanner_id IS NOT NULL) /* -- not-null-ignore */
+ )
+ ) NOT VALID;
+ """,
+ ),
+ # 1337 re-adds this as a constraint over the index 1336 builds concurrently.
+ migrations.RunSQL(
+ sql="""
+ ALTER TABLE "posthog_taggeditem" DROP CONSTRAINT IF EXISTS "posthog_taggeditem_tag_id_dashboard_id_insi_replay_scan_uniq";
+ """,
+ reverse_sql=migrations.RunSQL.noop,
+ ),
+ ],
+ ),
+ ]
diff --git a/posthog/migrations/1336_taggeditem_project_indexes.py b/posthog/migrations/1336_taggeditem_project_indexes.py
new file mode 100644
index 000000000000..146bc5cba979
--- /dev/null
+++ b/posthog/migrations/1336_taggeditem_project_indexes.py
@@ -0,0 +1,40 @@
+from django.db import migrations
+
+from posthog.migration_helpers import CreateIndexConcurrently
+
+
+class Migration(migrations.Migration):
+ # CONCURRENTLY can't run inside a transaction.
+ atomic = False
+
+ dependencies = [
+ ("posthog", "1335_taggeditem_project"),
+ ]
+
+ # `CreateIndexConcurrently` drops any invalid leftover from an interrupted build before
+ # retrying, so a transient cancellation can't wedge 1337's ADD CONSTRAINT USING INDEX.
+ # Django state for these indexes and constraints was added in 1335.
+ operations = [
+ CreateIndexConcurrently(
+ index_name="posthog_taggeditem_project_id_0a61d235",
+ table_name="posthog_taggeditem",
+ columns='("project_id")',
+ ),
+ CreateIndexConcurrently(
+ index_name="unique_project_tagged_item",
+ table_name="posthog_taggeditem",
+ columns='("tag_id", "project_id")',
+ unique=True,
+ where='WHERE "project_id" IS NOT NULL',
+ ),
+ CreateIndexConcurrently(
+ index_name="posthog_taggeditem_tag_id_dashboard_id_insi_4ec15a8f_uniq",
+ table_name="posthog_taggeditem",
+ columns=(
+ '("tag_id", "dashboard_id", "insight_id", "event_definition_id", "property_definition_id", '
+ '"action_id", "feature_flag_id", "experiment_saved_metric_id", "ticket_id", "account_id", '
+ '"endpoint_id", "replay_scanner_id", "project_id")'
+ ),
+ unique=True,
+ ),
+ ]
diff --git a/posthog/migrations/1337_taggeditem_project_unique_constraint.py b/posthog/migrations/1337_taggeditem_project_unique_constraint.py
new file mode 100644
index 000000000000..b898dc4d1f77
--- /dev/null
+++ b/posthog/migrations/1337_taggeditem_project_unique_constraint.py
@@ -0,0 +1,39 @@
+from django.db import migrations
+
+
+class Migration(migrations.Migration):
+ dependencies = [
+ ("posthog", "1336_taggeditem_project_indexes"),
+ ]
+
+ operations = [
+ migrations.RunSQL(
+ sql="""
+ ALTER TABLE "posthog_taggeditem" ADD CONSTRAINT "posthog_taggeditem_tag_id_dashboard_id_insi_4ec15a8f_uniq"
+ UNIQUE USING INDEX "posthog_taggeditem_tag_id_dashboard_id_insi_4ec15a8f_uniq"; -- existing-table-constraint-ignore
+ """,
+ # The reverse restores the replay-scanner-wide constraint that 1335 dropped with a noop
+ # reverse. It must build the index itself: this migration unapplies before 1336, so
+ # nothing else has.
+ reverse_sql="""
+ ALTER TABLE "posthog_taggeditem" DROP CONSTRAINT IF EXISTS "posthog_taggeditem_tag_id_dashboard_id_insi_4ec15a8f_uniq";
+ CREATE UNIQUE INDEX IF NOT EXISTS "posthog_taggeditem_tag_id_dashboard_id_insi_replay_scan_uniq"
+ ON "posthog_taggeditem" (
+ "tag_id",
+ "dashboard_id",
+ "insight_id",
+ "event_definition_id",
+ "property_definition_id",
+ "action_id",
+ "feature_flag_id",
+ "experiment_saved_metric_id",
+ "ticket_id",
+ "account_id",
+ "endpoint_id",
+ "replay_scanner_id"
+ );
+ ALTER TABLE "posthog_taggeditem" ADD CONSTRAINT "posthog_taggeditem_tag_id_dashboard_id_insi_replay_scan_uniq"
+ UNIQUE USING INDEX "posthog_taggeditem_tag_id_dashboard_id_insi_replay_scan_uniq"; -- existing-table-constraint-ignore
+ """,
+ ),
+ ]
diff --git a/posthog/migrations/1338_taggeditem_project_fk.py b/posthog/migrations/1338_taggeditem_project_fk.py
new file mode 100644
index 000000000000..f818f473811d
--- /dev/null
+++ b/posthog/migrations/1338_taggeditem_project_fk.py
@@ -0,0 +1,21 @@
+from django.db import migrations
+
+from posthog.migration_helpers import AddForeignKeyNotValid
+
+
+class Migration(migrations.Migration):
+ dependencies = [
+ ("posthog", "1337_taggeditem_project_unique_constraint"),
+ ]
+
+ # NOT VALID skips the child-row scan, so the lock this takes on posthog_project is a brief
+ # metadata-only ALTER rather than one held across a full scan. 1339 validates it lock-free.
+ operations = [
+ AddForeignKeyNotValid(
+ model_name="taggeditem",
+ name="posthog_taggeditem_project_id_fk",
+ column="project_id",
+ to_table="posthog_project",
+ to_column="id",
+ ),
+ ]
diff --git a/posthog/migrations/1339_validate_taggeditem_project_fk.py b/posthog/migrations/1339_validate_taggeditem_project_fk.py
new file mode 100644
index 000000000000..86b43d9fcef3
--- /dev/null
+++ b/posthog/migrations/1339_validate_taggeditem_project_fk.py
@@ -0,0 +1,17 @@
+from django.db import migrations
+
+from posthog.migration_helpers import ValidateForeignKey
+
+
+class Migration(migrations.Migration):
+ dependencies = [
+ ("posthog", "1338_taggeditem_project_fk"),
+ ]
+
+ # VALIDATE scans under SHARE UPDATE EXCLUSIVE, so it does not block reads or writes.
+ operations = [
+ ValidateForeignKey(
+ model_name="taggeditem",
+ name="posthog_taggeditem_project_id_fk",
+ ),
+ ]
diff --git a/posthog/migrations/max_migration.txt b/posthog/migrations/max_migration.txt
index 91e66cb69b23..d3750d0b1c1d 100644
--- a/posthog/migrations/max_migration.txt
+++ b/posthog/migrations/max_migration.txt
@@ -1 +1 @@
-1334_remove_cimd_blocklist_entry
+1339_validate_taggeditem_project_fk
diff --git a/posthog/models/project.py b/posthog/models/project.py
index 87c2461bdf46..1b381d228ec5 100644
--- a/posthog/models/project.py
+++ b/posthog/models/project.py
@@ -100,6 +100,15 @@ class Project(UpdatedMetaFields):
__repr__ = sane_repr("id", "name")
+ @property
+ def team_id(self) -> int:
+ """The id of this project's passthrough team, which a project shares.
+
+ Tag rows are team-scoped, so the shared tagging helpers reach the right namespace
+ through this attribute without loading the team.
+ """
+ return self.pk
+
@cached_property
def passthrough_team(self) -> "Team":
return self.teams.get(pk=self.pk)
diff --git a/posthog/models/tagged_item.py b/posthog/models/tagged_item.py
index 23ac4d38a225..0ebc5e9e7aa3 100644
--- a/posthog/models/tagged_item.py
+++ b/posthog/models/tagged_item.py
@@ -16,6 +16,7 @@ RELATED_OBJECTS = (
"account",
"endpoint",
"replay_scanner",
+ "project",
)
@@ -115,6 +116,17 @@ class TaggedItem(ModelActivityMixin, UUIDTModel):
blank=True,
related_name="tagged_items",
)
+ project = models.ForeignKey(
+ "posthog.Project",
+ on_delete=models.CASCADE,
+ null=True,
+ blank=True,
+ related_name="tagged_items",
+ # posthog_project is read on nearly every request, so creating this FK's database
+ # constraint inline would lock it. A later migration adds the constraint NOT VALID
+ # and validates it separately.
+ db_constraint=False,
+ )
class Meta:
unique_together = ("tag", *RELATED_OBJECTS)
diff --git a/posthog/tasks/test/__snapshots__/test_process_scheduled_changes.ambr b/posthog/tasks/test/__snapshots__/test_process_scheduled_changes.ambr
index 7c22347d3764..b6587c4b3a8b 100644
--- a/posthog/tasks/test/__snapshots__/test_process_scheduled_changes.ambr
+++ b/posthog/tasks/test/__snapshots__/test_process_scheduled_changes.ambr
@@ -296,7 +296,8 @@
"posthog_taggeditem"."ticket_id",
"posthog_taggeditem"."account_id",
"posthog_taggeditem"."endpoint_id",
- "posthog_taggeditem"."replay_scanner_id"
+ "posthog_taggeditem"."replay_scanner_id",
+ "posthog_taggeditem"."project_id"
FROM "posthog_taggeditem"
WHERE "posthog_taggeditem"."feature_flag_id" = 99999
'''
@@ -315,7 +316,8 @@
"posthog_taggeditem"."ticket_id",
"posthog_taggeditem"."account_id",
"posthog_taggeditem"."endpoint_id",
- "posthog_taggeditem"."replay_scanner_id"
+ "posthog_taggeditem"."replay_scanner_id",
+ "posthog_taggeditem"."project_id"
FROM "posthog_taggeditem"
WHERE "posthog_taggeditem"."feature_flag_id" = 99999
'''
@@ -1213,7 +1215,8 @@
"posthog_taggeditem"."ticket_id",
"posthog_taggeditem"."account_id",
"posthog_taggeditem"."endpoint_id",
- "posthog_taggeditem"."replay_scanner_id"
+ "posthog_taggeditem"."replay_scanner_id",
+ "posthog_taggeditem"."project_id"
FROM "posthog_taggeditem"
WHERE "posthog_taggeditem"."feature_flag_id" = 99999
'''
@@ -1232,7 +1235,8 @@
"posthog_taggeditem"."ticket_id",
"posthog_taggeditem"."account_id",
"posthog_taggeditem"."endpoint_id",
- "posthog_taggeditem"."replay_scanner_id"
+ "posthog_taggeditem"."replay_scanner_id",
+ "posthog_taggeditem"."project_id"
FROM "posthog_taggeditem"
WHERE "posthog_taggeditem"."feature_flag_id" = 99999
'''
diff --git a/products/actions/backend/api/test/__snapshots__/test_action.ambr b/products/actions/backend/api/test/__snapshots__/test_action.ambr
index 880aa8d77143..5c05a4e712e9 100644
--- a/products/actions/backend/api/test/__snapshots__/test_action.ambr
+++ b/products/actions/backend/api/test/__snapshots__/test_action.ambr
@@ -476,6 +476,7 @@
"posthog_taggeditem"."account_id",
"posthog_taggeditem"."endpoint_id",
"posthog_taggeditem"."replay_scanner_id",
+ "posthog_taggeditem"."project_id",
"posthog_tag"."id",
"posthog_tag"."name",
"posthog_tag"."team_id"
@@ -1107,6 +1108,7 @@
"posthog_taggeditem"."account_id",
"posthog_taggeditem"."endpoint_id",
"posthog_taggeditem"."replay_scanner_id",
+ "posthog_taggeditem"."project_id",
"posthog_tag"."id",
"posthog_tag"."name",
"posthog_tag"."team_id"
diff --git a/products/feature_flags/backend/api/test/__snapshots__/test_feature_flag.ambr b/products/feature_flags/backend/api/test/__snapshots__/test_feature_flag.ambr
index 37d9cad892b5..11ae2135b111 100644
--- a/products/feature_flags/backend/api/test/__snapshots__/test_feature_flag.ambr
+++ b/products/feature_flags/backend/api/test/__snapshots__/test_feature_flag.ambr
@@ -798,6 +798,7 @@
"posthog_taggeditem"."account_id",
"posthog_taggeditem"."endpoint_id",
"posthog_taggeditem"."replay_scanner_id",
+ "posthog_taggeditem"."project_id",
"posthog_tag"."id",
"posthog_tag"."name",
"posthog_tag"."team_id"
diff --git a/products/product_analytics/backend/tests/api/__snapshots__/test_insight.ambr b/products/product_analytics/backend/tests/api/__snapshots__/test_insight.ambr
index a705b12406c8..79edca68beb6 100644
--- a/products/product_analytics/backend/tests/api/__snapshots__/test_insight.ambr
+++ b/products/product_analytics/backend/tests/api/__snapshots__/test_insight.ambr
@@ -1514,7 +1514,8 @@
"posthog_taggeditem"."ticket_id",
"posthog_taggeditem"."account_id",
"posthog_taggeditem"."endpoint_id",
- "posthog_taggeditem"."replay_scanner_id"
+ "posthog_taggeditem"."replay_scanner_id",
+ "posthog_taggeditem"."project_id"
FROM "posthog_taggeditem"
WHERE "posthog_taggeditem"."insight_id" IN (1,
2,
@@ -1593,6 +1594,7 @@
"posthog_taggeditem"."account_id",
"posthog_taggeditem"."endpoint_id",
"posthog_taggeditem"."replay_scanner_id",
+ "posthog_taggeditem"."project_id",
"posthog_tag"."id",
"posthog_tag"."name",
"posthog_tag"."team_id"