Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(Persons): Remove foreign key constraints from Persons and related models #27636

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

timgl
Copy link
Collaborator

@timgl timgl commented Jan 17, 2025

Problem

We want to move persons and related models into its own DB. In order to do that, we need to remove foreign key constraints.

Changes

  • In the below set of models (which we'll move to the new DB), change all foreign keys that don't reference each other (ie Team, cohort, feature flag) into ints, so that we can only move these tables over (or vice versa)
    1. Person
    2. PersonDistinctId
    3. PersonlessDistinctId
    4. PersonOverrideMapping
    5. PersonOverride
    6. PendingPersonOverride
    7. FlatPersonOverride
    8. FeatureFlagHashKeyOverride
    9. CohortPeople

👉 Stay up-to-date with PostHog coding conventions for a smoother review.

Does this work well for both Cloud and self-hosted?

How did you test this code?

Copy link

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

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

PR Summary

This PR removes foreign key constraints from Person-related models, replacing them with integer fields to support database separation and improve scalability.

  • Removes Django admin interfaces for Person and PersonDistinctId models in /posthog/admin/admins/person_admin.py and /posthog/admin/admins/person_distinct_id_admin.py
  • Replaces ForeignKey fields with IntegerField (team_id, is_user_id) in /posthog/models/person/person.py
  • Removes ManyToManyField 'people' from Cohort model and updates CohortPeople to use cohort_id in /posthog/models/cohort/cohort.py
  • Updates FeatureFlagHashKeyOverride unique constraint to use team_id instead of team in /posthog/models/feature_flag/feature_flag.py

8 file(s) reviewed, 3 comment(s)
Edit PR Review Bot Settings | Greptile

Comment on lines +374 to +376
team_id = (
models.IntegerField()
) # Note: in order to move persons to separate database, this is a int instead of foreingkey
Copy link

Choose a reason for hiding this comment

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

style: Consider adding a db_index=True to team_id since it was previously indexed as a foreign key

@@ -376,7 +375,7 @@ def get_and_update_pending_version(cohort: Cohort):

class CohortPeople(models.Model):
id = models.BigAutoField(primary_key=True)
cohort = models.ForeignKey("Cohort", on_delete=models.CASCADE)
cohort_id = models.IntegerField()
Copy link

Choose a reason for hiding this comment

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

logic: removing foreign key constraint allows invalid cohort_ids to be inserted - consider adding application-level validation

Comment on lines +40 to +42
team_id = models.IntegerField()
properties = models.JSONField(default=dict)
is_user = models.ForeignKey("User", on_delete=models.CASCADE, null=True, blank=True)
is_user_id = models.IntegerField(null=True, blank=True)
Copy link

Choose a reason for hiding this comment

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

logic: Removing ForeignKey constraints means application code must ensure team_id and is_user_id values are valid. Consider adding validation in save() method.

@timgl timgl marked this pull request as draft January 17, 2025 01:57
@timgl timgl requested a review from benjackwhite January 17, 2025 01:57
@@ -37,9 +37,9 @@ class Person(models.Model):
# used for evaluating if we need to override the value or not (value: set or set_once)
properties_last_operation = models.JSONField(null=True, blank=True)

team = models.ForeignKey("Team", on_delete=models.CASCADE)
team_id = models.IntegerField()
Copy link
Contributor

Choose a reason for hiding this comment

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

Generally this all makes sense. Long term I think we will want to ditch the Django models alltogether and handle migrations from the node service instead (or whatever service "owns" Persons).

I guess we lose cascading deleted here although I'm not sure if that ever actually happened?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants