Database owner-scope pattern | DevPrompt Lab
Encode ownership checks and deletion behavior in queries and relational constraints.
rule · Database · advanced
English Tiếng ViệtA persistence rule for records that must remain private to their owner and preserve referenced history.
Reusable content
For user-owned records, include the owner or tenant key in reads, updates, and deletes. Add foreign keys and unique constraints for invariants the database can enforce. Choose deletion behavior intentionally: cascade only for private dependent data, set null for optional attribution, and restrict deletion when saved or public references must survive. Use transactions for related writes. Test cross-owner access, duplicate identities, and referenced-record deletion.
Owner key: {{TENANT_KEY}}
Deletion policy: {{DELETE_POLICY}}
How to use it
Document which records are public, private, archived, or legally retained before selecting foreign-key behavior.
Example
Saved-resource FK uses ON DELETE RESTRICT, while a user's own saved rows cascade when that user account is removed.