Relationships_
Manage complex data relationships with Appwrite Databases. Discover how to define and work with relationships between documents for interconnected data.
7 min read
Relationships describe how documents in different collections are associated, so that related documents can be read, updated, or deleted together. Entities in real-life often associate with each other in an organic and logical way, like a person and their dog, an album and its songs, or friends in a social network.
These types of association between entities can be modeled in Appwrite using relationships.
Relationship Attributes
Relationships are represented in a collection using relationship attributes. The relationship attribute contains the ID of related documents, which it references during read, update, and delete operations. This attribute is null if a document has no related documents.
When to use a relationship
Relationships help reduce redundant information. For example, a user can create many posts in your app. You can model this without relationships by keeping a copy of the user's information in all the documents representing posts, but this creates a lot of duplicate information in your database about the user.
Benefits of relationships
Duplicated records waste storage, but more importantly, makes the database much harder to maintain. If the user changes their user name, you will have to update dozens or hundreds of records, a problem commonly known as an update anomaly in databases. You can avoid duplicate information by storing users and posts in separate collections and relating a user and their posts through a relationship.
Tradeoff
Consider using relationships when the same information is found in multiple places to avoid duplicates. However, relationships come with the tradeoff of slowing down queries. For applications where the best read and write performance is important, it may be acceptable to tolerate duplicate data.
Opt-in Loading
By default, Appwrite returns only a document's own fields when you retrieve documents. Related documents are not automatically loaded unless you explicitly request them using query selection. This eliminates unintentional payload bloat and gives you precise control over performance.
Directionality
Appwrite relationships can be one-way or two-way.
| Type | Description |
|---|---|
| One-way | The relationship is only visible to one side of the relation. This is similar to a tree data structure. |
| Two-way | The relationship is visible to both sides of the relationship. This is similar to a graph data structure. |
Types
Appwrite provides four different relationship types to enforce different associative rules between documents.
| Type | Description |
|---|---|
| One-to-one | A document can only be related to one and only one document. |
| One-to-many | A document can be related to many other documents. |
| Many-to-one | Many documents can be related to a single document. |
| Many-to-many | A document can be related to many other documents. |
On-delete
Appwrite also allows you to define the behavior of a relationship when a document is deleted.
| Type | Description |
|---|---|
| Restrict | If a document has at least one related document, it cannot be deleted. |
| Cascade | If a document has related documents, when it is deleted, the related documents are also deleted. |
| Set null | If a document has related documents, when it is deleted, the related documents are kept with their relationship attribute set to null. |
Creating relationships
You can define relationships in the Appwrite Console, or using a Server SDK
Creating documents
If a collection has relationship attributes, you can create documents in two ways. You create both parent and child at the same time using a nested syntax or link parent and child documents through references*.
Queries
You can use filter queries directly against relationship attributes using dot notation. This lets you filter documents based on the values of their related documents, such as filtering posts by an author's name or filtering orders by a product's category.
Use the format relationshipKey.field to reference fields on related documents.
All filter queries are supported on relationship fields, including equal, notEqual, greaterThan, lessThan, between, contains, and other comparison operators.
Update Relationships
Relationships can be updated by updating the relationship attribute.
Delete relationships
Unlink relationships, retain documents
If you need to unlink documents in a relationship but retain the documents, you can do this by updating the relationship attribute and removing the ID of the related document.
If a document can be related to only one document, you can delete the relationship by setting the relationship attribute to null.
If a document can be related to more than one document, you can delete the relationship by setting the relationship attribute to an empty list.
Delete relationships and documents
If you need to delete the documents as well as unlink the relationship, the approach depends on the on-delete behavior of a relationship.
If the on-delete behavior is restrict, the link between the documents needs to be deleted first before the documents can be deleted individually.
If the on-delete behavior is set null, deleting a document will leave related documents in place with their relationship attribute set to null. If you wish to also delete related documents, they must be deleted individually.
If the on-delete behavior is cascade, deleting the parent documents also deletes related child documents, except for many-to-one relationships. In many-to-one relationships, there are multiple parent documents related to a single child document, and when the child document is deleted, the parents are deleted in cascade.
Permissions
To access documents in a relationship, you must have permission to access both the parent and child documents.
When creating both the parent and child documents, the child document will inherit permissions from its parent.
You can also provide explicit permissions to the child document if they should be different from their parent.
When creating, updating, or deleting in a relationship, you must have permission to access all documents referenced. If the user does not have read permission to any document, an exception will be thrown.
Limitations
Relationships can be nested between collections, but are restricted to a max depth of three levels. Relationship attribute key, type, and directionality can't be updated. On-delete behavior is the only option that can be updated for relationship attributes.
Was this page helpful?
Share what worked or what we should fix. Once approved, our agents automatically apply suggested updates to the docs.