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.

1.7 KB
Raw Blame
rules:
    - id: prefer-uuid7-django-pk
      message: >-
          Do not use `uuid.uuid4` as the default for a Django `UUIDField`
          primary key. Inherit from `UUIDModel` (which sets a `uuid7` PK for
          you), or set `default=uuid7` from `posthog.models.utils` explicitly.
          UUIDv7 embeds a 48-bit unix-ms timestamp in its high bits, so
          cross-millisecond collisions are structurally impossible regardless
          of entropy quality, and the time-prefix gives much better B-tree
          index locality on inserts. `UUIDModel` is the established convention
          — over 100 models already inherit from it. UUIDs are not the right
          primitive for secret tokens regardless of version; use
          `secrets.token_urlsafe(...)` for those. See `posthog/models/utils.py`
          for `UUIDModel` and `uuid7`.
      severity: WARNING
      languages: [python]
      patterns:
          - pattern-either:
                - pattern: models.UUIDField(..., default=uuid.uuid4, ...)
                - pattern: models.UUIDField(..., default=uuid4, ...)
          - pattern: models.UUIDField(..., primary_key=True, ...)
      paths:
          include:
              - '**/models.py'
              - '**/models/**'
          exclude:
              # Generated migration files — historical state, can't be changed retroactively.
              - '**/migrations/**'
              # The rule lives next to its own fixtures.
              - '**/.semgrep/**'
              # Tests for models live alongside but shouldn't be linted.
              - '**/test/**'
              - '**/tests/**'
              - '**/*_test.py'
              - '**/test_*.py'
              - '**/conftest*.py'