Let's say I'm building a chat which has groups and rooms. I need to create relational database scheme for it.
- Each group can have multiple rooms.
- And each group always has exactly 1 main room (which is also the same room as other)
- Room has already prebuilt (despite it's main room or not) structure and can't be duplicated. Like foreign keys to it, or description, notification settings etc.
So since it's one to many relationship the following db schema could represent this model:
,-----------------.
|Group |
|-----------------|
|*id: pk |
|*name: string |
|*main_room_id: fk|
`-----------------'
↑ ?
? ↓
,-------------.
|Room |
|-------------|
|*id: |
|*name: string|
|*notif: bool |
|*group_id: fk|
`-------------'
But I'd like to avoid circular reference in this scheme. I can do this by having boolean field in a room that says that this is a main room:
,-------------.
|Group |
|-------------|
|*id: pk |
|*name: string|
`-------------'
↑
?
,----------------.
|Room |
|----------------|
|*id: |
|*name: string |
|*notif: bool |
|*group_id: fk |
|*is_main: bool |
`----------------'
But in using this scheme database is not normalized. Main room is_main
boolean field depends on others room is_main
field. Furthermore more complex constrain should be created to support data consistency.
What is the appropriate database schema for this issue?
question from:https://stackoverflow.com/questions/65876118/how-to-remove-a-circular-reference-from-relational-database