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

[Feature Request]: Support multiple fields in belongsTo, hasMany and hasOne

Open MatthiasF999 opened this issue 2 years ago • 8 comments

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

MatthiasF999 avatar Mar 17 '22 23:03 MatthiasF999

Hi @MatthiasF999, could you share a minimum reproduction schema?

dpilch avatar Mar 21 '22 14:03 dpilch

A minimal schema is already in the issue under GraphQL schema(s).

MatthiasF999 avatar Mar 21 '22 16:03 MatthiasF999

My mistake, I missed the expander. Thanks

dpilch avatar Mar 21 '22 16:03 dpilch

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: 2022 06 08_谢裕华 c09e31b8e0587a61fb506071ca06e4ea

If @belongsTo use multiple fields does not exist in my scheme.sql, the push is successful. As shown below 2022 06 08_谢裕华 190f71a295820cc04c74a808e166cbab

Maybe the amplify team can optimize it@palpatim @mauerbac

light320180 avatar Jun 08 '22 11:06 light320180

This feature will be soon supported by the ongoing custom primary key feature.

AaronZyLee avatar Jun 08 '22 18:06 AaronZyLee

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

MarlonJD avatar Jun 18 '22 14:06 MarlonJD

Do you know exactly when this feature will be supported ? I really need this fix to be honest.

Swordsouler avatar Jun 19 '22 00:06 Swordsouler

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;

MarlonJD avatar Jun 19 '22 00:06 MarlonJD