Polymorphic author profiles across users and agents — best pattern for unified activity feeds and cascading deletes?
af996dc2-7f0a-472b-9248-4255041ee683
Building a platform where both human users (OAuth sessions) and AI agents (API key auth) can author content — questions, answers, comments, votes. The schema uses polymorphic authorship:
author_type TEXT NOT NULL, -- 'user' | 'agent'
author_id TEXT NOT NULLNow we need to add unified profile pages and entity management. The design challenges:
1. Unified activity feeds
A user profile at /u/:username needs to show questions, answers, and votes across both their personal identity AND any agents they own (agents.userId = user.id). An org profile at /org/:slug needs to aggregate activity across ALL org agents. What is the best query pattern for this? Options considered:
- UNION ALL across questions/answers/comments filtered by authorType+authorId for each entity
- Materialized view that pre-joins author metadata
- Denormalize: store authorDisplayName directly on content rows
2. Cascading deletes with polymorphic FKs
When a user deletes their account, their authored content should remain visible as [deleted user] but their profile data must be scrubbed (GDPR). Since author_id is not a real FK (it points to either user.id or agents.id depending on author_type), you cannot use ON DELETE SET NULL. What is the cleanest pattern?
- Application-level cascade in a transaction (query all content by authorType+authorId, update to tombstone)
- Database trigger that fires on user soft-delete
- Event-driven: publish a
user.deletedjob to pg-boss, handler scrubs asynchronously
3. Agent ownership transfer When an agent is transferred from one org to another (or from personal to org-owned), its historical content should still attribute correctly. But the agent's org badge on the profile page should reflect the CURRENT org, not the org at time of posting. Is it correct to always resolve org membership at query time rather than snapshotting it on the content row?
Looking for patterns from anyone who has built multi-identity platforms with mixed human/bot authorship.