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';

<Fragments fragments={{ js: js0 }} />

import reactnative0 from "/src/fragments/lib/datastore/js/relational/updated-schema.mdx";

<Fragments fragments={{'react-native': reactnative0}} />

import ios1 from "/src/fragments/lib/datastore/ios/relational/updated-schema.mdx";

<Fragments fragments={{ ios: ios1 }} />

import android2 from '/src/fragments/lib/datastore/android/relational/updated-schema.mdx';

<Fragments fragments={{ android: android2 }} />

import flutter3 from '/src/fragments/lib/datastore/flutter/relational/updated-schema.mdx';

<Fragments fragments={{ flutter: flutter3 }} />

## 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';

<Fragments fragments={{ js: js4 }} />

import reactnative1 from "/src/fragments/lib/datastore/js/relational/save-snippet.mdx";

<Fragments fragments={{'react-native': reactnative1}} />

import ios5 from "/src/fragments/lib/datastore/ios/relational/save-snippet.mdx";

<Fragments fragments={{ ios: ios5 }} />

import android6 from '/src/fragments/lib/datastore/android/relational/save-snippet.mdx';

<Fragments fragments={{ android: android6 }} />

import flutter7 from '/src/fragments/lib/datastore/flutter/relational/save-snippet.mdx';

<Fragments fragments={{ flutter: flutter7 }} />

## Querying relations

import js8 from '/src/fragments/lib/datastore/js/relational/query-snippet.mdx';

<Fragments fragments={{ js: js8 }} />

import reactnative2 from "/src/fragments/lib/datastore/js/relational/query-snippet.mdx";

<Fragments fragments={{'react-native': reactnative2}} />

import ios9 from "/src/fragments/lib/datastore/ios/relational/query-snippet.mdx";

<Fragments fragments={{ ios: ios9 }} />

import android10 from '/src/fragments/lib/datastore/android/relational/query-snippet.mdx';

<Fragments fragments={{ android: android10 }} />

import flutter11 from '/src/fragments/lib/datastore/flutter/relational/query-snippet.mdx';

<Fragments fragments={{ flutter: flutter11 }} />

## 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';

<Fragments fragments={{ js: js12 }} />

import reactnative4 from "/src/fragments/lib/datastore/js/relational/delete-snippet.mdx";

<Fragments fragments={{'react-native': reactnative4}} />

import ios13 from "/src/fragments/lib/datastore/ios/relational/delete-snippet.mdx";

<Fragments fragments={{ ios: ios13 }} />

import android14 from '/src/fragments/lib/datastore/android/relational/delete-snippet.mdx';

<Fragments fragments={{ android: android14 }} />

import flutter15 from '/src/fragments/lib/datastore/flutter/relational/delete-snippet.mdx';

<Fragments fragments={{ flutter: flutter15 }} />

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.

<Callout warning>

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.

</Callout>

```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';

<Fragments fragments={{ js: js16 }} />


import reactnative5 from "/src/fragments/lib/datastore/js/relational/save-many-snippet.mdx";

<Fragments fragments={{'react-native': reactnative5}} />

import ios17 from '/src/fragments/lib/datastore/ios/relational/save-many-snippet.mdx';

<Fragments fragments={{ ios: ios17 }} />

import android18 from '/src/fragments/lib/datastore/android/relational/save-many-snippet.mdx';

<Fragments fragments={{ android: android18 }} />

import flutter19 from '/src/fragments/lib/datastore/flutter/relational/save-many-snippet.mdx';

<Fragments fragments={{ flutter: flutter19 }} />

import js20 from '/src/fragments/lib/datastore/js/relational/query-many-snippet.mdx';

<Fragments fragments={{js: js20}} />

import reactnative6 from "/src/fragments/lib/datastore/js/relational/query-many-snippet.mdx";

<Fragments fragments={{'react-native': reactnative6}} />