Thought I can handle it all by myself, but... I thik I've wasted too much time... Hello everyone! I'm struggling with Room database structure - I'm not sure if wheter my approach is correct or not. What I'm trying to achieve is to be able to add Services (sub-subcategories) to Categories, but there may be optional Subcategories as well:
Category -> Service
Category -> optional Subcategory -> Service
What I've already did (skipped some basic parts of code):
a) created entities
@Entity
class Category
int id
String name
@Entity
class Subcategory
int id
String name
int categoryID
@Entity
class Subcategory
int id
String name
int categoryID
int subcategoryID
b) created join table
@Entity
class Join
int id
int categoryID
int subcategoryID
int serviceID
It's straight-forward when a category has subcategory with any services:
id | categoryId | SubcategoryId| ServiceId|
-------------------------------------------
1 | 1 | 1 | 1 |
1 | 1 | 1 | 2 |
but in case there's no subcategory and service belongs to category only:
id | categoryId | SubcategoryId| ServiceId|
-------------------------------------------
1 | 1 | null | 1 |
I put null
as SubcategoryId and check if subcategory is null
So the question is: is this approach correct in terms of relational database design ?
Or should I: 1. create category-subcategory, subcategory-service, category-service join tables and conncet them somehow eventually in one, big join ? 2. or add categoryId and subcategoryId to the service ?
Any comments, tips & tricks much appreciated! :)