Is it always bad practice to have circular relationships in database design?

I don't know how to solve this problem I have the following ratio

user has -> many messages

One post has โ†’ many comments

one comment has -> one user (owner)

enter image description here

I keep reading this argument by database developers since it shouldn't be done, but I've never read any solutions to these problems, for example:

This month, instead of starting with an idea and creating a model to support the concept, I will analyze the poor design of the project: the situation I call a circular reference. A circular reference is a recursive condition in which one table refers to a second table, which in turn refers to the first table.

Is it possible to have a circular link in this case?

+3


source to share


2 answers


I agree with the comment from @DavidAldridge and I edited the image in the question to explain what the comment indicates.

As far as circular references in databases go, my experience is that you can create them and simple queries and updates will work, but as soon as you need a more complex join, you end up creating variants of Cartesian joins and quickly consume a lot of memory and query results will never be returned.



And note that a circular reference is not the same as a self-reference relationship. Self-rated tables are a common sight in database design. You can find a simple example here: Self-Referencing Tables or a complete tutorial on all Link Types, including self-promotion.

Database diagram

+1


source


If two "users" had the same meaning, I would say that you violated the "no redundant information" principle. But the "owner" of the comment is not necessarily the "owner" of the post. This way you are not storing a single user redundantly.

Relationships don't have to be tree-shaped. If there was some rule that the relationship should only be DAG (Directed Acyclical Graphs), then the question makes sense.

Let's look at the data in a different way. You have entities: users, posts, comments. You have the relationship that you mentioned. So who cares about the "circuit"?



You may even have "users" "married" to each other. Couldn't be much cooler than this!

But then we don't have to do our homework, especially for more than a year.

0


source







All Articles