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

When creating a type with a sort key on the id field, codegen is making id required #639

Open
focomoso opened this issue Jul 24, 2023 · 4 comments
Labels
bug Something isn't working p2 transferred Issue has been transferred from another Amplify repository

Comments

@focomoso
Copy link

Amplify CLI Version

12.1.1

Question

When creating a type in schema.graphql like this

type NodeModel
  @model
  @auth(rules: [{ allow: private}])
{
  id: ID! @primaryKey
  ...
}

codegen creates a type like this:

type LazyNodeModel = {
  readonly [__modelMeta__]: {
    identifier: OptionallyManagedIdentifier<NodeModel, 'id'>;
  };
  readonly id: string;
  ...
}

Which allows me to do:

const node = new NodeModel({
  // id not required
  // other fields...
})

The ID is not required.

But if I add a sort key to the primary key like this:

type NodeModel
  @model
  @auth(rules: [{ allow: private}])
{
 id: ID! @primaryKey(sortKeyFields: ["someOtherID"])
 ...
}

The generated type is:

type LazyNodeModel = {
  readonly [__modelMeta__]: {
    identifier: CompositeIdentifier<NodeModel, ['id', 'someOtherID']>;
  };
  readonly id: string;
  ...
}

And when I try to do:

const node = new NodeModel({
  id: 'some-value', // typescript complains if I don't add this
  // other fields...
});

It's requiring that I specify the ID myself.

Is this a bug or am I misusing the @primarayKey and sortKeyFields directives?

@focomoso focomoso added pending-triage Issues that need further discussion to determine question Further information is requested labels Jul 24, 2023
@ykethan
Copy link

ykethan commented Jul 24, 2023

Hey, 👋 thanks for raising this! I'm going to transfer this over to our codegen repository for better assistance 🙂

@ykethan ykethan transferred this issue from aws-amplify/amplify-cli Jul 24, 2023
@ykethan ykethan added the transferred Issue has been transferred from another Amplify repository label Jul 24, 2023
@dpilch
Copy link
Contributor

dpilch commented Aug 1, 2023

This appears to be a bug. id should be auto-populated in the @primaryKey on id field with sort key case.

https://docs.amplify.aws/lib/datastore/advanced-workflows/q/platform/js/#determine-when-the-primary-key-field-is-auto-populated-upon-record-creation

@dpilch dpilch added bug Something isn't working p2 and removed question Further information is requested pending-triage Issues that need further discussion to determine labels Aug 1, 2023
@gregorsiwinski
Copy link

gregorsiwinski commented Aug 21, 2023

This is a breaking change for us, had to roll back to older versions which worked fine.

@dpilch
Copy link
Contributor

dpilch commented Sep 5, 2023

Dependent on aws-amplify/amplify-js#11707

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working p2 transferred Issue has been transferred from another Amplify repository
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants