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

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

Open stephenjen opened this issue 1 year ago • 2 comments

Description

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

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:

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

stephenjen avatar Nov 03 '23 05:11 stephenjen

Hello @stephenjen - I was able to reproduce this issue. I am going to transfer this issue to our code gen repo.

Jordan-Nelson avatar Nov 09 '23 19:11 Jordan-Nelson

@Jordan-Nelson Thanks for pushing this forward

stephenjen avatar Nov 10 '23 01:11 stephenjen