aws-amplify / aws-amplify/amplify-codegen

Custom primary key @primaryKey doesn’t work with @auth

Open
#753 2 comments 0 reactions 0 assignees View on GitHub
bug transferred
Dominant language
TypeScript
Stars
59
Forks
64
PR merge metrics
No merged PRs in 30d

Description

### Description

I have two parent / child tables, and the parent table has custom primary key:

```graphql
type Book @model @auth(rules: [{ allow: private, ownerField: "pOwner" }]) {
id: ID! @primaryKey(sortKeyFields: ["title", "description"])
title: String!
description: String!
writers: [Writer] @hasMany(indexName: "byBook", fields: ["id"])
pOwner: String!
}

type Writer @model {
id: ID!
fname: String!
lname: String!
bookID: ID! @index(name: "byBook", sortKeyFields: ["id"])
book: Book! @belongsTo(fields: ["bookID"])
pOwner: String!
}
```

When I add the same @auth to the child, I get the following error on attempt to generate the model with 'amplify codegen models':

`InvalidDirectiveError: Invalid @belongsTo on Writer:book. Provided fields do not match the size of primary key(s) for Book`

Below are the models that cause this error:

```graphql
type Book @model @auth(rules: [{ allow: private, ownerField: "pOwner" }]) {
id: ID! @primaryKey(sortKeyFields: ["title", "description"])
title: String!
description: String!
writers: [Writer] @hasMany(indexName: "byBook", fields: ["id"])
pOwner: String!
}

type Writer @model @auth(rules: [{ allow: private, ownerField: "pOwner" }]) {
id: ID!
fname: String!
lname: String!
bookID: ID! @index(name: "byBook", sortKeyFields: ["id"])
book: Book! @belongsTo(fields: ["bookID"])
pOwner: String!
}
```

### Categories

- [ ] Analytics
- [ ] API (REST)
- [ ] API (GraphQL)
- [ ] Auth
- [ ] Authenticator
- [X] DataStore
- [ ] Notifications (Push)
- [ ] Storage

### Steps to Reproduce

_No response_

### Screenshots

_No response_

### Platforms

- [ ] iOS
- [ ] Android
- [ ] Web
- [X] macOS
- [ ] Windows
- [ ] Linux

### Flutter Version

3.13.5

### Amplify Flutter Version

1.5.0

### Deployment Method

Amplify CLI

### Schema

_No response_

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.