Back

I have read the Relationships and i didn't understand well

  • 0
  • Databases
  • General
  • Cloud
Ahmed CODE
26 Jun, 2024, 15:40

i'm trying to transfer my subcollections data in my firebase firestore data to appwrite

TL;DR
A developer is using Two-way relationships but is confused about deleting documents in Firestore. They are unsure about the relationship structure and are transferring subcollection data to Appwrite. They are facing an issue when passing document Ids for relationships in Appwrite, as they are required to pass data as an object instead of just the docId. Solution: In Appwrite, when dealing with relationships, pass the document Ids in an object format to maintain the relationships effectively. For example, utilize a structure like: ```js await databases.createDocument( DATABASE_ID, COLLECTION_ID, DOCUMENT_ID, { name: 'Something
Ahmed CODE
26 Jun, 2024, 15:41

with a new logic that appwrite cloud support

Kenny
26 Jun, 2024, 15:42

What is the data structure of documentData

Ahmed CODE
26 Jun, 2024, 15:44

it's different from subcollection to subcollection, anyway this is an example:

first subcollection

TypeScript
{
      "date": "2024-05-08",
      "city": "xx",
      "imageURl": "https://cdn.discordapp.com/attachments/xxxxx",
      "name": "xxxx",
      "id": "xx",
      "age": "xx",
      "firestoreId": "1197596471621599302"
    },

second:

TypeScript
    {
      "bankAmount": 10001500,
      "blocked": false,
      "cashAmount": 0,
      "id": "xxxx",
      "firestoreId": "1027912017685782600"
    },
Ahmed CODE
26 Jun, 2024, 15:44

and another two subcollections that looks something like this

Kenny
26 Jun, 2024, 15:45

I don't believe you would use the collectionId for the relationship, you would use the attribute name.

Ahmed CODE
26 Jun, 2024, 15:45

firestoreId = document id in firestore

Kenny
26 Jun, 2024, 15:45
Ahmed CODE
26 Jun, 2024, 15:45

yh i did

Ahmed CODE
26 Jun, 2024, 15:46

you can check:

TypeScript
                switch (appwriteCollectionId) {
                    case 'bank':
                        relationshipData = 'activeUsers'
                        continue;
                    case 'active_users':
                        relationshipData = 'activeUsers'
                        continue;
                    case 'police_penalties':
                        relationshipData = 'policePenalties'
                        continue;
                    case 'ids':
                        relationshipData = 'iDs'
                        continue;
                }
Kenny
26 Jun, 2024, 15:46

alright, because in the code it says relationshipData = appwriteCollectionId

Kenny
26 Jun, 2024, 15:46

ah I see that now, didn't go far enough down

Kenny
26 Jun, 2024, 15:50

on line 383, can that document you're updating have multiple relationships?

Ahmed CODE
26 Jun, 2024, 15:51

yh

Ahmed CODE
26 Jun, 2024, 15:51

it has relationship with all other subCollections

Ahmed CODE
26 Jun, 2024, 15:52

as i said it's 4 subcollections that were in my firestore data

Ahmed CODE
26 Jun, 2024, 15:52

this is why i handled the switch logic

Kenny
26 Jun, 2024, 15:52

Make docId an array of one

Kenny
26 Jun, 2024, 15:53

If the relationship is many, I believe it has to be an array. If it's just one to one then it's a string of the id.

Ahmed CODE
26 Jun, 2024, 15:54

i'll try

Ahmed CODE
26 Jun, 2024, 15:54

as all of them are one-to-many

Ahmed CODE
26 Jun, 2024, 15:54

also i wanna ask if i'm not gonna annoy you, i'm using Two-way relationship -> "one-to-many" with On deleting a document = CasCade

do this mean if i deleted a document inside the related collection is going to delete the main one?

Kenny
26 Jun, 2024, 15:59

if you delete the parent it will delete the children

Ahmed CODE
26 Jun, 2024, 15:59

and if i deleted the child?

Ahmed CODE
26 Jun, 2024, 15:59

as i'm using Two-way relationship not one way

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