Back

[SOLVED] How to create relationship within the same collection?

  • 0
  • Databases
manazo
23 Sep, 2023, 16:14

I want to create a relationship in my "Users" collection that'll point to it's own self. Tho I'm not building a social media app, but you can think of it as the friends list of a user in a social media network. So each user doc will need a relationship named "friends" to its own collection ("Users" in this case), cuz each "friend" is also a user. When I try doing it from the console, I can't find the "Users" collection under the "Related Collection" search box.

I don't know if I was clear enough... Let me know if you want more details

TL;DR
The user wants to create a relationship within the same collection. They mention using a two-way many-to-many relationship, but another user suggests using a two-way many-to-one relationship instead. The issue is that the "Users" collection is not showing up in the console's "Related Collection" search box. Solution: The user can create a friendship collection and use relationship attributes to link it to the user collection. However, arrays are not supported in Appwrite, so they may need to find an alternative method for managing the relationship.
Drake
23 Sep, 2023, 16:16

Ya...you can't at the moment. You can either create a friendship collection and then have relationship attributes to the user collection.

manazo
23 Sep, 2023, 16:23

wouldn't it make it harder to manage? and what will the friendship collection contain?

Drake
23 Sep, 2023, 16:26

I don't think so. I also usually avoid arrays because Appwrite doesn't support array operations

manazo
23 Sep, 2023, 16:27

what the friendship collection should contain?

Drake
23 Sep, 2023, 16:33

A two way many to one. This would allow you to see all the friends a user has from the user collection. Then, a one way many to one. Actually...this works more for a follower following scenario 🤔

manazo
23 Sep, 2023, 21:29

do you mean, that whoever is added as a friend to anyone, should be added to the "friends" collection with a two-way many-to-one relationship with "Users"?

Drake
23 Sep, 2023, 21:33

Yes, but there should be two attributes.

Maybe two documents should be added for each triend

manazo
23 Sep, 2023, 21:37

about attributes, we need to store all the necessary info of the friend along with the relationship attribute. is there any more important attribute in your mind?

manazo
23 Sep, 2023, 21:37

Maybe two documents should be added for each triend why two documents?

Drake
25 Sep, 2023, 05:05

So that you can make 1 query to get all the friends of a user

manazo
25 Sep, 2023, 06:19

if we do two-way many-to-many, then can't we just get all the friends of a user directly from the user doc? only need to get the corresponding user document and read the "friends" attribute?

Drake
25 Sep, 2023, 21:32

sure i guess.

manazo
26 Sep, 2023, 06:36

[SOLVED] How to create relationship within the same collection?

Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more