amplify-swift
amplify-swift copied to clipboard
How can I create @manyToMany relation with the same model
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.
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.
Hi. Yes, that's exactly what I'm trying to achieve and that is the error I got as well. Thanks.
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
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!
Please track https://github.com/aws-amplify/amplify-category-api/issues/609