Are two way relations a thing in PayloadCMS?

default discord avatar
rxbbxss
2 months ago
4

EG: If I have a collection, say SportsTeams, with a relation to another collection Matches, is there a reccomended way of setting up so the Matches know which Sports teams they belong to, and the sports teams know which matches they have?



I might be missing something obvious here.

  • default discord avatar
    paradox5270
    2 months ago

    Personally I would have a property “teams” in Matches collection that stores array of id (has many) relation to SportsTeams, then turn on indexing for the “teams” field. That way when querying for matches a team has played, you simply use where: {teams: {contains: team_id}}

  • discord user avatar
    seanzubrickas
    Payload Team
    last month

    Hey @rxbbxss did you try the suggestion @paradox5270 made here? Let us know if that works for you or if you need additional help here. Thanks!

  • default discord avatar
    rxbbxss
    last month

    Ended up using a different db setup for that project as there were going to be a lot of two way bindings! Sorry forgot to mark this as closed



    ... And I'm not sure how to tbh!

  • discord user avatar
    seanzubrickas
    Payload Team
    last month

    No worries! I'll take care of it

Open the post
Continue the discussion in Discord
Like what we're doing?
Star us on GitHub!

Star

Connect with the Payload Community on Discord

Discord

online

Can't find what you're looking for?

Get help straight from the Payload team with an Enterprise License.