4
I am working on a system where the previous programmer structured a particular table of posts referencing itself (to be able to identify what would be the post comment)
Sort of like this:
Posts
-- id
-- post_id => Referencia de Post (ela mesma)
-- texto
-- usuario_id
What had post_id would be comments from the related post; and what it lacked was the normal post.
In this case, I already know that this structure is completely wrong, because it should have been created a table called comentarios
with foreign key defined in posts
But outside of this case, there are other cases where there is need to reference the table itself with a foreign key (I’ve never seen this)?
I don’t know if you’re completely wrong. Maybe is not the best solution for this case but is perfectly valid. Do not stick to theories. If the comment table has the same structure as this one, why create a different table with the same structure? It may be useful to give more semantics to the data but other than that, there’s not much advantage.
– Maniero
I thought it might be an anti-pattern because the translated name "Foreign key" was "foreign key". I mean, I don’t see any foreignness in "self-relativeness"
– Wallace Maxters
@bigown I am totally in favor of new practices, after all where the existing standards came from?
– Edi. Gomes
@Edi.Gomes currently in most cases in the book industry, courses, seminars, consultancies and tools :P
– Maniero
Related or duplicate? http://answall.com/questions/23029/para-que-serve-um-self-join
– bfavaretto
@bfavaretto Related huh, here he did not know that it was a relationship and certainly is a question about data modeling, in the other question the user wants to know about a command using the same.
– Edi. Gomes
One is a question of whether it’s wrong or not; and the other is searching for understanding about how to do such a "self Join"
– Wallace Maxters