amplify-swift icon indicating copy to clipboard operation
amplify-swift copied to clipboard

How can I create @manyToMany relation with the same model

Open alionthego opened this issue 2 years ago • 3 comments

I have read the documentation on creating a manyToMany relation on two unique models using GraphQLAPI which suggests the following schema:

type Post @model {
  id: ID!
  title: String!
  content: String
  tags: [Tag] @manyToMany(relationName: "PostTags")
}

type Tag @model {
  id: ID!
  label: String!
  posts: [Post] @manyToMany(relationName: "PostTags")
}

However, I want to create a manyToMany between users in an app. So when a user adds another user as a friend or blocks a user etc, there will be a userToUsers entry. So I would like manyToMany relation on the same model but have not been able to do that.

alionthego avatar May 15 '22 23:05 alionthego

Hi @alionthego, thanks for opening this.

Can you confirm that this is the issue you're experiencing? For a schema like this:

type Post @model {
  id: ID!
  title: String!
  posts: [Post] @manyToMany(relationName: "Posts")
}

amplify codegen models generates the following error: InvalidDirectiveError: @manyToMany relation '' must be used in exactly two locations.

The @manyToMany(...) directive creates a many-to-many relationship between two distinct models by creating a join table to perform queries on. So it won't work for your use case. I'll check to see if we have guidance on how to handle this type of use case and circle back to you here.

atierian avatar May 19 '22 20:05 atierian

Hi. Yes, that's exactly what I'm trying to achieve and that is the error I got as well. Thanks.

alionthego avatar May 19 '22 22:05 alionthego

Hi @alionthego, that's unfortunately a limitation in the current version of the CLI/Codegen. You can track the feature request we've opened here https://github.com/aws-amplify/amplify-category-api/issues/609

diegocstn avatar Jun 10 '22 23:06 diegocstn

This has been identified as a feature request. If this feature is important to you, we strongly encourage you to give a 👍 reaction on the request. This helps us prioritize new features most important to you. Thank you!

github-actions[bot] avatar Oct 31 '22 19:10 github-actions[bot]

Please track https://github.com/aws-amplify/amplify-category-api/issues/609

lawmicha avatar Aug 10 '23 16:08 lawmicha