Skip to content

Swift Model Codegen for Unidirectional Has-Many AssociatedFields Missing Index fields #539

@lawmicha

Description

@lawmicha

In this PR #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

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 is not picking up "postTitle" and returning it in the CodeGenField[].

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingp2

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions