aws-amplify / aws-amplify/amplify-codegen
Swift Model Codegen for Unidirectional Has-Many AssociatedFields Missing Index fields
- Dominant language
- TypeScript
- Stars
- 59
- Forks
- 64
- PR merge metrics
- No merged PRs in 30d
Description
In this PR https://github.com/aws-amplify/amplify-codegen/pull/538 we're populating the `associatedFields` of a has-many connection. There is one case that doesn't seem to be working as expected. For this schema:
```
// Uni-directional has-many with CPK on parent, with customized FK
type Post8 @model {
postId: ID! @primaryKey(sortKeyFields:["title"])
title: String!
comments: [Comment8] @hasMany(indexName:"byPost", fields:["postId", "title"])
}
type Comment8 @model {
commentId: ID! @primaryKey(sortKeyFields:["content"])
content: String!
postId: ID @index(name: "byPost", sortKeyFields:["postTitle"]) # customized foreign key for parent primary key
postTitle: String # customized foreign key for parent sort key
}
```
The output of Post's schema file contains `.hasMany(comments)`
```
.hasMany(post.comments, is: .optional, ofType: Comment.self, associatedFields: [Comment.keys.postId])
```
This output [doesn't look accurate](https://github.com/aws-amplify/amplify-codegen/pull/538/files/1cbf6b159f29de1a48a6c2947e878f38b4ece051#r1095218053)
The associatedFields of the comment to the post should be the foreign key fields on the Comment "postId" and "postTitle".
The expected output, to my understanding, should be:
```
.hasMany(post.comments, is: .optional, ofType: Comment.self, associatedFields: [Comment.keys.postId, Comment.keys.postTitle])
```
Opening this issue to bring awareness to see if we can collaborate on the solution. The code is getting pretty complicated to debug. My initial hunch is that somewhere in [`getConnectedFieldsForHasMany` ](https://github.com/aws-amplify/amplify-codegen/blob/main/packages/appsync-modelgen-plugin/src/utils/process-has-many.ts#L55) is not picking up "postTitle" and returning it in the `CodeGenField[]`.
Contributor guide
Assessment
This issue has not been assessed yet.