DataStore has the capability to handle relationships between Models, such as _has one_, _has many_, _belongs to_. In GraphQL this is done with the `@hasOne`, `@hasMany` and `@index` directives as defined in the [GraphQL Transformer documentation](/cli/graphql/data-modeling).
## Updated schema
import js0 from '/src/fragments/lib/datastore/js/relational/updated-schema.mdx';
import reactnative0 from "/src/fragments/lib/datastore/js/relational/updated-schema.mdx";
import ios1 from "/src/fragments/lib/datastore/ios/relational/updated-schema.mdx";
import android2 from '/src/fragments/lib/datastore/android/relational/updated-schema.mdx';
import flutter3 from '/src/fragments/lib/datastore/flutter/relational/updated-schema.mdx';
## Saving relations
In order to save connected models, you will create an instance of the model you wish to connect and pass its ID to `DataStore.save`:
import js4 from '/src/fragments/lib/datastore/js/relational/save-snippet.mdx';
import reactnative1 from "/src/fragments/lib/datastore/js/relational/save-snippet.mdx";
import ios5 from "/src/fragments/lib/datastore/ios/relational/save-snippet.mdx";
import android6 from '/src/fragments/lib/datastore/android/relational/save-snippet.mdx';
import flutter7 from '/src/fragments/lib/datastore/flutter/relational/save-snippet.mdx';
## Querying relations
import js8 from '/src/fragments/lib/datastore/js/relational/query-snippet.mdx';
import reactnative2 from "/src/fragments/lib/datastore/js/relational/query-snippet.mdx";
import ios9 from "/src/fragments/lib/datastore/ios/relational/query-snippet.mdx";
import android10 from '/src/fragments/lib/datastore/android/relational/query-snippet.mdx';
import flutter11 from '/src/fragments/lib/datastore/flutter/relational/query-snippet.mdx';
## Deleting relations
When you delete a parent object in a one-to-many relationship, the children will also be removed from the DataStore and mutations for this deletion will be synced to cloud. For example, the following operation would remove the Post with id *123* as well as any related comments:
import js12 from '/src/fragments/lib/datastore/js/relational/delete-snippet.mdx';
import reactnative4 from "/src/fragments/lib/datastore/js/relational/delete-snippet.mdx";
import ios13 from "/src/fragments/lib/datastore/ios/relational/delete-snippet.mdx";
import android14 from '/src/fragments/lib/datastore/android/relational/delete-snippet.mdx';
import flutter15 from '/src/fragments/lib/datastore/flutter/relational/delete-snippet.mdx';
However, in a many-to-many relationship the children are not removed and you must explicitly delete them.
## Many-to-many relationships
For many-to-many relationships, you can use the `@manyToMany` directive and specify a `relationName`. Under the hood, Amplify creates a join table and a one-to-many relationship from both models.
Join table records must be deleted prior to deleting the associated records. For example, for a many-to-many relationship between Posts and Tags, delete the PostTags join record prior to deleting a Post or Tag.
```graphql
enum PostStatus {
ACTIVE
INACTIVE
}
type Post @model {
id: ID!
title: String!
rating: Int
status: PostStatus
editors: [User] @manyToMany(relationName: "PostEditor")
}
type User @model {
id: ID!
username: String!
posts: [Post] @manyToMany(relationName: "PostEditor")
}
```
import js16 from '/src/fragments/lib/datastore/js/relational/save-many-snippet.mdx';
import reactnative5 from "/src/fragments/lib/datastore/js/relational/save-many-snippet.mdx";
import ios17 from '/src/fragments/lib/datastore/ios/relational/save-many-snippet.mdx';
import android18 from '/src/fragments/lib/datastore/android/relational/save-many-snippet.mdx';
import flutter19 from '/src/fragments/lib/datastore/flutter/relational/save-many-snippet.mdx';
import js20 from '/src/fragments/lib/datastore/js/relational/query-many-snippet.mdx';
import reactnative6 from "/src/fragments/lib/datastore/js/relational/query-many-snippet.mdx";