amplify-codegen
amplify-codegen copied to clipboard
[Feature Request]: Support multiple fields in belongsTo, hasMany and hasOne
Before opening, please confirm:
- [X] I have installed the latest version of the Amplify CLI (see above), and confirmed that the issue still persists.
- [X] I have searched for duplicate or closed issues.
- [X] I have read the guide for submitting bug reports.
- [X] I have done my best to include a minimal, self-contained set of instructions for consistently reproducing the issue.
How did you install the Amplify CLI?
npm
If applicable, what version of Node.js are you using?
v16.14.0
Amplify CLI Version
7.6.25
What operating system are you using?
Windows
Amplify Codegen Command
codegen models
Describe the bug
Having multiple fields results in the error DataStore only support one key in field. Using belongsTo and hasOne with composite keys is therefore not possible.
Expected behavior
Generate the models with composite keys.
Reproduction steps
This is a new feature request.
GraphQL schema(s)
# Put schemas below this line
type Post @model {
tenant: ID! @primaryKey(sortKeyFields: ["id"])
id: ID!
title: String!
comments: [Comment] @hasMany(indexName: "byPost", fields: ["tenant", "id"])
}
type Comment @model {
tenant: ID!
@primaryKey(sortKeyFields: ["id"])
@index(name: "byPost", sortKeyFields: ["postID"])
id: ID!
postID: ID!
content: String!
post: Post @belongsTo(fields: ["tenant", "postID"])
}
Log output
# Put your logs below this line
Additional information
No response
Hi @MatthiasF999, could you share a minimum reproduction schema?
A minimal schema is already in the issue under GraphQL schema(s).
My mistake, I missed the expander. Thanks
I also encountered this error. But I perform data validation and query. This exception is thrown when we execute the "amplify push" command to push and does not affect the query, you can still query. E.g: Post has multiple comments comment has corresponding Post
But for fear of other influences or instability, I still removed the way @belongsTo uses multiple fields.
If @belongsTo uses multiple fields in my scheme.sql, the error is as follows:
If @belongsTo use multiple fields does not exist in my scheme.sql, the push is successful. As shown below
Maybe the amplify team can optimize it@palpatim @mauerbac
This feature will be soon supported by the ongoing custom primary key feature.
Using multiple belongsTo in one model, I tried to change to add multiple belongsTo on variables but, somehow multiple belongsTo broke toJson in related model. If you fix toJson, I can add my pull request for this
Do you know exactly when this feature will be supported ? I really need this fix to be honest.
I created new GraphQLRequest<T> and add my modified and missing id to it then using this for mutation. I just lookup belongsTo and other ID's variable creation on the amolify_api and i modified there but i couldn't handle json decode, don't know how I couldn't parse other id.it's there but i cannot get the id. İt's interesting. Anyway i temporarily fix by adding missing id by hand. If you think about to fix this issue like this, i can add example for you
final mainDepartmentStudentObj = MainDepartmentStudent(
department: registerSelectedDepartmentController!,
student: studentCreateResponse.data!,
);
GraphQLRequest<MainDepartmentStudent> mutation =
ModelMutations.create(mainDepartmentStudentObj);
Map<String, dynamic> newMutationVariables = {
"input": {
"departmentID": registerSelectedDepartmentController?.id,
"studentID": studentCreateResponse.data?.id,
"id": mutation.variables.entries.first.value["id"]
}
};
GraphQLRequest<MainDepartmentStudent> newMutation = GraphQLRequest(
document: mutation.document,
apiName: mutation.apiName,
variables: newMutationVariables,
decodePath: mutation.decodePath,
modelType: mutation.modelType);
final mainDepartmentStudentCreateResponse =
await Amplify.API.mutate(request: newMutation).response;