---
title: "Discussion on \"Enforcing Multi-Tenant Isolation Structurally, Not by Convention\" | Hashnode"
description: "Discussion on \"Enforcing Multi-Tenant Isolation Structurally, Not by Convention\". Originally published at nazmulkhalid.com.\nTL;DR: Relying on every query to remember WHERE schoolId = ... doesn't survive a real codebase. Bicitra EMS injects the tenant scope automatically with a Pris"
url: "https://hashnode.com/posts/enforcing-multi-tenant-isolation-structurally-not-by-convention/6ad225758674ee5ef382956c"
---

[Feed](https://hashnode.com/feed) /

Discussion

# [Enforcing Multi-Tenant Isolation Structurally, Not by Convention](https://technonazmul.hashnode.dev/enforcing-multi-tenant-isolation-structurally-not-by-convention?utm_source=hashnode&utm_medium=feed)

Originally published at nazmulkhalid.com. TL;DR: Relying on every query to remember WHERE schoolId = ... doesn't survive a real codebase. Bicitra EMS injects the tenant scope automatically with a Pris

Oct 16 · 4 min read · technonazmul.hashnode.dev  
Read the article

-   [#saas](https://hashnode.com/tag/saas)
-   [#databases](https://hashnode.com/tag/databases)
-   [#security](https://hashnode.com/tag/security)
-   [#postgresql](https://hashnode.com/tag/postgresql)

## Written by

[Nazmul Khalid](https://hashnode.com/@technonazmul)

@technonazmul · Dhaka, Bangladesh

  
[View profile](https://hashnode.com/@technonazmul)

Lead Software Engineer: SaaS, MVPs and DevOps for founders

Lead Software Engineer with 10+ years of experience. I build multi-tenant SaaS, MVPs (web + Flutter apps) and production DevOps for founders and small teams worldwide. Lead engineer on DocBee and BD Doctors Directory. Work with me: https://nazmulkhalid.com/hire

## Responses 3

Markdown supported · ⌘↵ to post

[Kamran Khan](https://hashnode.com/@kamrankhandev)

[Oct 18](https://hashnode.com/posts/enforcing-multi-tenant-isolation-structurally-not-by-convention/6ad225758674ee5ef382956c/comment/6ad4c6db716bb43fe5ddf7d4)

The Laravel version of this exact failure is the global scope everyone adds to the tenant models and then quietly bypasses in the admin panel — one withoutGlobalScopes() in an export, one raw DB::select in a report, and the same class of leak opens. The registry idea is the part I'd steal: structural enforcement validated at write time rather than a scope applied at read time. StackShortlist's test suggestion is the missing piece, honestly. Without a failing test on registry drift, the registry becomes a convention with extra steps — and the eighth endpoint is only safe until someone adds the ninth relation by hand.

[StackShortlist](https://hashnode.com/@veristria)

[Oct 16](https://hashnode.com/posts/enforcing-multi-tenant-isolation-structurally-not-by-convention/6ad225758674ee5ef382956c/comment/6ad239f6e6252b7cb31552ea)

The nested include gap is a great catch. Putting tenant-scoped foreign keys in one registry and checking them at write time means the eighth endpoint is safe by default. Did you also add a test that fails if a new relation gets added without a registry entry?

[Nazmul Khalid](https://hashnode.com/@technonazmul)

[Oct 17](https://hashnode.com/posts/enforcing-multi-tenant-isolation-structurally-not-by-convention/6ad225758674ee5ef382956c/comment/6ad239f6e6252b7cb31552ea#reply-6ad3187a091f555ae98c9ab3)

Not yet, and that's a fair gap to point at. Right now, adding a tenant-scoped relation means adding it to the registry by hand. The file's header comment says so, but that's still convention. The test I have in mind reads Prisma's schema metadata, finds every foreign key between two tenant-scoped models, and fails if one is missing from the registry. That turns the last manual step into a CI failure.
