Back

what is the correct way to update a child document part of relation?

  • 0
  • Self Hosted
  • Databases
dhoomakethu
3 Sep, 2023, 01:20

Hi, I am trying to get hold of using relationships with Python SDK (appwrite 1.3.7). I was successfully able to create and retrieve the documents. However I am having problems with updating the created document. If I don't pass the $id attribute along with the child payload, duplicates are being created. If I pass $id along with the payload it results in user unauthorised to perform this action error. Passing only IDs as an array works as expected but what if these child documents also has some updates? Do we have to update them separately and update the parent ?

Please note the user creating the documents has read/update/delete permission on both parent and child.

The relation is many to many.

TL;DR
The user is experiencing issues when trying to update a child document in a relation using the Python SDK (appwrite 1.3.7). If the `$id` attribute is not passed with the child payload, duplicates are created. If the `$id` is passed, an "unauthorized to perform this action" error occurs. The user wants to know if there is a way to update both the parent and child documents together. The relation is many-to-many. Solution: To update the child document, include the `$id` attribute in the nested JSON. Ensure that the user has read/update/delete permissions on both the parent and child
Drake
3 Sep, 2023, 01:38

weird..you should only get that error if the user doesn't have access to the child document.... you definitely need to include $id in the nested JSON..

Drake
3 Sep, 2023, 01:38

what's your code (the one with the nested $id)?

dhoomakethu
3 Sep, 2023, 02:53

@Steven I am following the same example as in the docs

TypeScript
await databases.updateDocument(
    'marvel',
    'movies',
    'spiderman',
    {
        title: 'Spiderman',
        year: 2002,
        reviews: [
            {$id: <doc_id>, author: 'Bob', text: 'Great movie!' },
            { $id: <doc_id>, author: 'Alice', text: 'Loved it to the core!' },
            { author: 'Dave', text: 'Wonderful movie!' }
        ]
    }
);

Here the third review is the new one and the other two are old reviews that were created when the document was created. The text field has been updated in the existing child documents.

Drake
3 Sep, 2023, 02:57

Ya I would expect that to work...

Drake
3 Sep, 2023, 02:58

What are the permissions on the 2 collections?

dhoomakethu
3 Sep, 2023, 03:03

Same as the parent

Drake
3 Sep, 2023, 03:03

Screenshot please?

dhoomakethu
3 Sep, 2023, 03:04

They were created along with the parent in the createDocument call.

dhoomakethu
3 Sep, 2023, 03:05

On mobile, will share the screenshots and Python code in sometime. Thanks

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