Which Category is your question related to?
Datastore
Amplify CLI Version
4.18.0
What AWS Services are you utilizing?
API, Cognito
Provide additional details e.g. code snippets
Following schema used to work. But doesn't work anymore.
type Blog @model
@auth(rules: [
{allow: owner}
]) {
id: ID!
title: String!
owner: String!
comments: [Comment] @connection(keyName: "byBlog", fields: ["id"])
}
type Comment @model
@auth(rules: [
{allow: owner}
])
@key(name: "byBlog", fields: ["blogID"]) {
id: ID!
owner: String!
content: String!
blogID: ID!
}
Now if I do amplify codegen models
I get following error
Error: DataStore does not support connection directive with keyName
My question is
"Is this permanant change? or you have plans to support connection directive with keyName for datastore in future?"
I am having the same issue even though it was suggested here that it should work, and that the docs for DataStore are outdated.
The responsible code for this is here. I implemented this fix on my own as an experiment before finding this issue, and I can confirm that with this fix, the resulting models and schemas are generated fine.
"association": {
"connectionType": "HAS_MANY",
"associatedWith": "tag" // tag is a model
}
But of course I have no clue whether this is actually supported or not and whether something will break somewhere else down the line.
Can someone from the amplify team comment on this? It is super limiting to not be able to do this.
Just noting that in my previous comment I believed this to be limiting because I believed this was the only way to implement M-M connections. However, I was misled by the new docs as they had two sections, one of which uses keyName and another which does not.
after upgrading amplify CLI to version: @aws-amplify/[email protected]
I received this error:
_DataStore does not support connection directive with keyName_
@apoorvmote
My question is
"Is this permanant change? or you have plans to support connection directive with keyName for datastore in future?"
The DataStore client libraries never supported keyName, and model gen did not flag this. We updated the model gen to throw this as an error so that there are no surprises at later at run time.
This change is not permanent and we do have plans to support keyName
in future releases
I'm still confused on the recommended way to do many-to-many relationships when we want Datastore support.
@connection
with keyName
-> this was the previously recommended way from my understanding@connection
with name
parameter which was the "old way" and supposed to become obsolete@connection
I understand this is a complex issue, leading to several breaking changes. Keep up the great work! This library makes AWSÂ much more accessible.
@yuth is there an open issue tracking this future feature that we can track?
Kind of confusing, I thought the DataStore is designed for working with Graphql API.
So not supporting means DateStore is not designed for use with GraphQL API?
Or, it is WIP?
@Albert-Gao it can be confusing, but it is in fact supported by the legacy (and future-outdated name=""
directives.. The fact that the amplify add api
generates model schema based on the new directives keyName=""
is also very misleading. Such is the reality with distributed services supported by distributed teams - things won't always be in sync even though it would be great if they were!
I'd suggest looking at the docs here and using the soon-to-be-deprecated name=""
directives in place of @key
and keyName
directives. These seem to be supported and work. Although I personally have hit many speed bumps along the way.. specifically a very misleading error:
Error: Models are not topologically sortable. Please verify your schema.
@blydewright Thanks, now I have a working example now. :) But won't progress far since the issue here.
This is my 2nd try of Amplify, always stuck from the start for some feature. Hope this time it goes well.
@Albert-Gao I also have run into many issues, however most of them seem to have been me doing this the wrong way. I think some better documentation steps, especially showing the ideal flow and process for setting things up in live and mock environments, would really help! But the AWS guys seem to be overloaded which is understandable.
@Albert-Gao I managed to sort out the issue with the Error: Models are not topologically sortable. Please verify your schema.
error. It seemed that my schema didn't have valid relationship mappings.. The error was terribly misleading.
I've confirmed that there are definitely issues with DataStore in the mock environment. Better for you to run things in AWS cloud for now while these are addressed.
Can you take a look at the latest docs for Relational modeling with DataStore and use the latest library version? We made some improvements to the CLI, the library on how it handles this internally, and tried to be explicit about schemas in the documentation including how to use @key
.
@undefobj
When using the @key directive without a name property with DataStore, the first item in the fields array must be id, e.g. @key(fields: ["id", "content"]).
Does the field have to be named id or can it just be an ID field?
Would this work:
enum PostStatus {
ACTIVE
INACTIVE
}
type Post
@model
@key(fields: ["postID"])
{
postID: ID!
title: String!
rating: Int!
status: PostStatus!
# New field with @connection
comments: [Comment] @connection(keyName: "byPost", fields: ["postID"])
}
# New model
type Comment @model
@key(fields: ["commentID"])
@key(name: "byPost", fields: ["postID", "content"]) {
commentID: ID!
postID: ID!
content: String!
}
As long as the key has a name, it can be anything. But if the name property is omitted, the first item in the fields array must be “id”.
I updated to Amplify 4.20.0 with the following command. It works
npm i -g @aws-amplify/cli
Most helpful comment
after upgrading amplify CLI to version: @aws-amplify/[email protected]
I received this error:
_DataStore does not support connection directive with keyName_