I have the following table designed already.
Now the new requirement is i need to show some Category for all the
tenants. Also each tenant should able to add their new Category into
the Master Category. So they can see all master + their specific
categories
Tenant Table
TenantId
Name
Group Table
GroupId
Name
Category Table
CategoryId
Name
TenantXCategory
TenantId
CategoryId
What changes i can do in above tables to achieve it? I tried this below
Modified Category table to below.
Category Table
CategoryId
Name
TenantId NULL // This indicates tenant specific category
Add a unique key for TenantId and Name
Then queried
SELECT *
FROM Category where TenantId = 1
UNION
SELECT *
FROM Category where TenantId IS NULL
But the problem is if two tenant only want to see a particular
Category, I need to add a new row with other TenantId in Category
table. This mean i am creating duplicate entry in a lookup table. Any
suggestion to achieve the new requirement?
So, a tenant A may see:
Your present schema seems to satisfy the requirements. In particular, the first two rules can indeed be implemented using a nullable
TenantIdcolumn in theCategorytable, where NULL would stand for a master category and a non-NULL value would reference the creator/owner of the category and thus signify a tenant-specific category. (I might rename the column to something likeOwnerTenantIdfor better clarity, but that might be just me.)To retrieve only master categories or those that belong to the specified tenant, you could use the query you’ve posted in your question or this one (which will probably yield an identical execution plan to your query’s):
To implement the third rule, you could use your
TenantXCategorytable to store categories available to tenants in addition to those that are accessible using the first two rules. That is, if tenant M permits tenant N to see some of tenant M’s categories, the categories’ Ids would be inserted intoTenantXCategoryalong with tenant N’s Id.So, to query all categories available to a particular tenant, you could do something along the lines of the following: