Skip to content
Back

Function gives unauthorized error when writing relational attribute; it works when I comment it out

  • 0
  • Tools
  • Databases
  • Web
Los Feliz
6 Jun, 2025, 06:31

I am on free tier.

I have function that has three attributes:

  1. A stand alone attribute (required)
  2. A many to one relationship attribute
  3. A many to many relationship attribute

When I run createDocument(), I get the error about user not having the permission to perform that action. However, when I comment out the relationship attributes, and only fill out the stand alone attribute, the createDocument() function runs smoothly. I do not understand why. I am more confused because the relationship attributes in their respective collections get created. It is only in this particular function that gives the unauthorized error. I am very confused.

TypeScript
export const makePost = async (name, linksData, comment) => {

    console.log({ name, linksData, comment });

    try {
        const user = await createUser(name);
        // User gets created successfully 
     
        const links = await Promise.all(
            linksData.map(link =>
                createLink(link.href, link.companyName, link.item)
            )
        );
        // Links get created successfully 
  
        // gives error when user and link are not commented out,
        // runs smoothly when user and link are commented out
        const res = await databases.createDocument(
            dbEnv,
            postsCollEnv,
            ID.unique(),
            {
                user, // Many to one relationship
                links, // Many to many relationship
                comment // Required attribute
            }
        );

        console.log('Post created successfully:', res);

        return res ? res : null;

    } catch (error) {
        console.error('Error creating post:', error);
        return null;
    }
}
TL;DR
Function `makePost` gives unauthorized error when attempting to create a document with many-to-one and many-to-many relationship attributes. The error disappears when the relationship attributes are commented out. The issue likely stems from permission constraints with those specific database operations.
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