Database design for posts and comments

If there are many comments in one message, and the comments essentially coincide with the messages (for example, they have a headline, pictures and sound, etc.), should I create two tables or only one?

For example, if I use only one table, I may have a parent_id column, so if it is not an answer to something, it will be null, otherwise it will have the identifier of the parent message. On the other hand, I can create a column and a table of comments. Comments may also respond to another comment so that it can get confused quickly.

*Post* id title content image audio parent_id 

or,

 *Post* *Comments* id id title title content content image author_id audio post_id author_id image audio 

The second option is to create indexes. Infact I don’t even need to add author_id or post_id. If I use indexes from the very beginning, will I?

What do you think about this? What would be more effective? I am thinking about using redbeanphp for this.

+6
source share
2 answers

The second option will be better. When displaying a bulletin board, you do not care about the comments and view them using the column with the index column of the parent column. Posts and comments are likely to have different fields, so saving them will be correct. The parent id for the first option will work fine, but conceptually, it's messy, and you basically create an index to use in half or, like many other comments, regarding posts.

+2
source

As a rule, in : Tables are called objects, so each entity in your application must be separated and demonstrated by a table. Here, although you think that messages and comments have the same data type, but finally, each of them is a separate object, so they should be separated in two tables. Such behavior is not a personal opinion. This is a basic rule that leads to smoother application development.

0
source

Source: https://habr.com/ru/post/923926/


All Articles