Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Control foreign field name for @hasMany #9236

Closed
2 tasks
johnf opened this issue Dec 7, 2021 · 3 comments
Closed
2 tasks

Control foreign field name for @hasMany #9236

johnf opened this issue Dec 7, 2021 · 3 comments
Labels
@connection Issues tied to @connection directive feature-request Request a new feature

Comments

@johnf
Copy link
Contributor

johnf commented Dec 7, 2021

Is this feature request related to a new or existing Amplify category?

api

Is this related to another service?

No response

Describe the feature you'd like to request

This is a bug/feature

I can set the index on hasMany as follows

type User {
  is: ID!
  posts: [Posts] @hasMany(indexName: "byUser")
}

type Post {
  id: ID!
  userId: ID! @index(name: "byUser")
  user: User @belongsTo(fields: "userId")
}

If I look at the query generated for Post it will look something like

   query GetPost($id: ID!) {
      getPost(id: $id) {
        id
        userId
        user {
          id
          posts {
            nextToken
          }                                                                                                                                                                         
        }                                                                                                                                                                                    
        createdAt                                                                                                                                                                            
        updatedAt                                                                                                                                                                            
        userPostsId                                                                                                                                                                        
      }                                                                                                                                                                                      
    }                               

Notice that userPostsId is being created when it isn't needed.

Describe the solution you'd like

I would suggest two changes

  • Add the ability to set the foreign key name in hasMany
  • If an index is references in HasMany then use the fields for that index as the foreign key

Describe alternatives you've considered

Living with the autogenerated key names but this makes the code not as neat as it can be for my use case

Additional context

@edwardfoyle this looks like it might be similar to #8557

Is this something that you'd be interested in working on?

  • 👋 I may be able to implement this feature request
  • ⚠️ This feature might incur a breaking change
@akshbhu akshbhu added @connection Issues tied to @connection directive pending-triage Issue is pending triage labels Dec 7, 2021
@InnovateWithEric InnovateWithEric added feature-request Request a new feature and removed pending-triage Issue is pending triage labels Dec 7, 2021
@lazpavel
Copy link
Contributor

lazpavel commented Jan 4, 2022

@johnf you should be able to avoid the generation of the extra field by adding the fields attribute to the hasMany directive as in

type User @model {
  is: ID!
  posts: [Posts] @hasMany(indexName: "byUser", fields: ["is"])
}

Please let us know if this solved it for you

@josefaidt
Copy link
Contributor

Closing due to inactivity. @johnf if you are still experiencing this issue please reply back to this thread and we can re-open to investigate further 🙂

@github-actions
Copy link

This issue has been automatically locked since there hasn't been any recent activity after it was closed. Please open a new issue for related bugs.

Looking for a help forum? We recommend joining the Amplify Community Discord server *-help channels for those types of questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 23, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
@connection Issues tied to @connection directive feature-request Request a new feature
Projects
None yet
Development

No branches or pull requests

5 participants