Back

Relationships and security

  • 0
  • Databases
  • Cloud
StefanB
24 Nov, 2023, 21:39

Hence I have 2 collections:

  1. Contact(name, address) => collection security = create (for all users)
  2. Address(street, city) => collection security = none

Why do I need to give "create" permission to all related collections? What if I want, that the user only can create a contact and needs to use the existing addresses?

TL;DR
Summary: The user is asking about the relationship between documents in a collection and how to set permissions for creating and updating nested documents. The user also asks for clarification on the types of relationships, such as one-to-many and many-to-many. They also mention encountering a 401 error related to authorization. Another user suggests providing a reproducible example to further understand and resolve the issue. Solution: The user is advised to give "READ" and "UPDATE" permissions to the Address collection in order to create a document containing a nested document. Another user suggests providing a reproducible example to troubleshoot the 401 error related to authorization.
Drake
25 Nov, 2023, 00:40

Are you getting some kind of error? If so, what's your code?

StefanB
25 Nov, 2023, 07:33

I‘m getting 401 „user not authorized“ I create a document with the nested documents If needed, I can create a minimal reproducible example

Rovar2000
25 Nov, 2023, 08:23

Same i still don’t know how to fix this issue, has no problem with non relation documents.

Drake
25 Nov, 2023, 16:17

Yes, reproducible example would be great

StefanB
25 Nov, 2023, 16:51

Here is Flutter/Dart code and instructions

TypeScript
Future<void> main() async {
/// 1. create project and collections
  /// Collections
  /// Address(String city)
  /// Contact(String, name, Address address)
  ///
  /// 2. Activate document security
  /// 3. In collections settings of Contact: activate "CREATE" for all users
  ///
  /// 4. Create user
  /// 5. create Address
  /// 6. Add "READ" permission for user to created address

  final client = Client(endPoint: "https://cloud.appwrite.io/v1")
    ..setProject(projectId);
  final account = Account(client);
  final database = Databases(client);

  try {
    await account.get();
  } catch (e) {
    await account.createEmailSession(email: email, password: password);
  }

  final documentList = await database.listDocuments(
    databaseId: databaseId,
    collectionId: addressCollection,
  );
  final address = documentList.documents.first;
  print(address);

  final newContact = {"name": "new contact", "address": address.data};
  final response = await database.createDocument(
    databaseId: databaseId,
    collectionId: contactCollection,
    documentId: ID.unique(),
    data: newContact,
  );
  // AppwriteException: user_unauthorized, The current user is not authorized to perform the requested action. (401)
  print(response);
}
StefanB
25 Nov, 2023, 17:03

I figured out, that it works if we give "READ" and "UPDATE" to the Address collection

ideclon
26 Nov, 2023, 07:13

If your issue has been solved, you can mark this post as closed by adding “[SOLVED]” to the beginning of the title

StefanB
26 Nov, 2023, 09:20

But why do I need to give „UPDATE“ permissions

fafa
26 Nov, 2023, 10:54

This should only be needed if the user needs to send PATCH requests, if the user only needs to read just make it READ only

StefanB
26 Nov, 2023, 11:12

Ok Does not work I need to give UPDATE permission to create a document containing this nested document

fafa
26 Nov, 2023, 11:25

So.. you want to create a document?

StefanB
26 Nov, 2023, 11:50

I have already a document B Collection A has a Many-To-One relationship to B I want to create a document in collection A (that has the nested document B)

fafa
26 Nov, 2023, 11:52

Ah I see. You need the UPDATE permission because you are updating a relation within document B. You need the CREATE permission for collection A

StefanB
26 Nov, 2023, 11:58

Ok I understand

But did I understand the relation correct? I „only“ want a collection containing a document from another collection 🤔

fafa
26 Nov, 2023, 15:49

Could you explain a little more? I don't fully understand your question

StefanB
26 Nov, 2023, 19:07

Following example as mentioned above I have a collection „Address“ with street, postal code etc and a collection „Contact“ and this contact has a name and an address How to do that with appwrite relations (Multiple contacts can have the same address)

fafa
26 Nov, 2023, 19:10

Can a contact only have one address or multiple?

fafa
26 Nov, 2023, 19:11

If so and I understand correctly, it would be many to many.

StefanB
27 Nov, 2023, 06:31

And if a contact only can one address → one to many?

Osman
27 Nov, 2023, 07:30

If a contact can only be associated with 1 address, but an address can be associated with multiple contacts, then it's one to many

StefanB
29 Nov, 2023, 12:36

Ok, this works

StefanB
29 Nov, 2023, 12:37

Another question If I would delete a contact via appwrite dashboard, I get the dialog explaining, that the value for related documents get get set to null. Are there under the hood documents, that get created for every contact (related to one address) that gets created? Or where are these values set to null?

fafa
29 Nov, 2023, 13:04

It basically means, that the document is no longer visible at the other document where you set the relation to.

StefanB
30 Nov, 2023, 12:43

Ok, but I never see the document because it’s a one way relationship (Many to one)? Or did I misunderstood something?

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