I am new to Contentful and so far I love using the service.
I’m not sure how to structure my content models to implement a two-way relationships between models. Say I have Article A and Article B and I want to express the notion that “Article A is related to Article B” (consequently implying that Article B is also related to Article A).
I could add a “Related Articles” field of type “Link” on my “Article” model. But it doesn’t feel very robust.
Let’s go through a simple scenario:
- In the web editor, when editing Article A, I link to Article B from the “Related Articles” field.
- Then on my website, on the page displaying Article A, “Article B” will naturally show up in the “Related Articles” field. So far, so good.
- But what about the web page displaying Article B? I’d like “Article A” to show up in its “Related Articles” section but how does that work? I can certainly query for all articles linking to “the current article”, and “Article A” will be retrieved. But Article B could also have its own links in its “Related Articles” field. So now I have to merge the articles from the “Related Articles” field + the articles returned by the query. And what if I inadvertently also link from Article B to Article A, creating a double?
In traditional SQL, I would create a cross-reference table with the sole purpose of storing the relationship (see https://en.wikipedia.org/wiki/Many-to-many_(data_model)) but I’m not sure it’s the right thing to do with Contentful. Also, that would impose a burden on content creators who would have to edit two pieces of content instead of one (the article AND the relationship).
Any suggestion would be appreciated.