Answer the question
In order to leave comments, you need to log in
How to properly build a database architecture?
Good afternoon. There is the following task: there are User and Event entities (for example, some kind of concert). The User has an avatar, the Event has a preview, i.e. and there and there, a connection with the file is necessary, by and large 1: 1.
The File table has been created, and this is where the problems begin. It is not clear how to properly link the tables to each other. So far, I have added fields like id_user, id_event to the file, which will identify the associated entity, respectively, if the file is associated with a user, then id_event should be NULL. How true is this? And perhaps you could suggest a more elegant and correct solution.
I also understand that you can simply make 2 tables, like user_files and event_files - but in my opinion this is generally stupid. Thank you!
Answer the question
In order to leave comments, you need to log in
In the tables Event and User there should be a string field - the file name. And that's it. To fence additional tables is senseless to complicate that architecture of a DB.
If you really want to make a centralized storage of pictures - you do it in Event and User by the file_id field, referring to the File table. But there should be no references to Event and User in the File table at all. You need to select an image when displaying an entry - this is done by linking from Event/User to File. For what tasks might you need a reverse link?
NB To determine if a file belongs to a user, a connection from File to User is not required, a connection from User to File is sufficient.
Well, firstly, this is not stupidity, within the framework of the task it is very normal, since File is too general a thing. And if you need to make a many-to-many relationship to another table? Or will there be several previews for the event?
In short, either make two different tables, or start one common one and link them through many-to-many. Or yes, one table with the type field and two rage keys for the event and user tables.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question