Back

Inserting into relationship attribute show unrelated error message

  • 0
  • Self Hosted
  • Web
mav8rick
18 Mar, 2024, 19:27

On version 1.3.7

The context: collection A has a many to many relationship with collection B.

Using Server API, we can create documents in A with the "child-documents" of B in the same call provided the "child document" in the call does not already exist in collection B. You can provide the $id of the child-documents in the same call to create a document in A.

However, if a document - say, with $id = 'abc' - already exists in B, a call to create a document in A along with child-document of B with { $id: 'abc' } will cause a super-cryptic error:

TypeScript
[Error] Timestamp: 2024-03-18T16:28:05+00:00
[Error] Method: POST
[Error] URL: /v1/databases/:databaseId/collections/:collectionId/documents
[Error] Type: Appwrite\Extend\Exception
[Error] Message: The current user is not authorized to perform the requested action.
[Error] File: /usr/src/code/app/controllers/api/databases.php
[Error] Line: 2726

I almost thought I hit into database corruption again... which I'm trying to climb out of.

After investigation, the answer is that if the child-document already exists in collection B, then you can only "link" e.g. [ <doc ID a>, { $id: def}] where def has not been created but doc ID a is already in collection B.

I'm not sure what is the behavior in the later versions but I was expecting that if the call to createDocument along with those details, Appwrite should update if the same document ID can be be found in the "child collection". Even if not, I think the error was unexpected - hence the cryptic "current user is not authorized" error.

TL;DR
Developers on version 1.3.7 are experiencing a super-cryptic error when trying to create a document in Collection A with a child document from Collection B that already exists. The error message reads "The current user is not authorized to perform the requested action." The solution is to make sure to only "link" the child document that already exists in Collection B, instead of trying to create it again. It's recommended to check for updates in later versions regarding this behavior.
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