J
J
Jekson2021-02-15 12:04:34
PostgreSQL
Jekson, 2021-02-15 12:04:34

Architecture of relationships between tables in a database?

I build a relationship between tables. business logic is. There is an event (event table), from two to infinity users can take part in it (users table). After the end of the event, each of the participants can leave a review for another participant (feedbacks table) with a link to this event. So there are 3 tables. I have defined the links as follows:

events:
  id
  ....

users:
  id
  ...


feedbacks:
  id
  ivent_id(OnetoOne to events)
  from_id(ForeignKey to users)
  to_id(ForeignKey to users)


Is this structure correct and if not, how to do it optimally?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Kuzmichev, 2021-02-15
@Lepilov

events:
  id (PK)
  ....

users:
  id (PK)
  ...

event_users
  event_id (PK, FK to events)
  user_id (PK, FK to users)

feedbacks:
  id (PK)
  event_id (FK to events)
  user_id_from (FK to users)
  user_id_to (FK to users)
  
  idx_unique (event_id, user_id_from, user_id_to) - уникальный индекс

In event_users, each user for each event can be recorded only 1 time (because both fields are included in PK)
In feedbacks at the application level, you can add only those records where both user_id_from and user_id_to are present in event_users for this event.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question