Amplify-cli: Unable to add @key to an existing model

Created on 26 Feb 2020  Â·  2Comments  Â·  Source: aws-amplify/amplify-cli

Hey, Amplify team!

I was hoping to get some assistance around adding sorting to an existing app. I was trying to add the createdAt to the @key directive, according to this stackoverflow answer.

When adding @key to an existing type model - I get an error when trying to run aws publish.

Any help would be fantastic.

Thanks for this awesome tool, loving it so far!

Which Category is your question related to?

API

Amplify CLI Version

➜  amplify -v
4.13.4

What AWS Services are you utilizing?

| Category | Resource name | Operation | Provider plugin |
| -------- | ------------------------ | --------- | ----------------- |
| Api | XXXXXXXXXXXXXXXX | Update | awscloudformation |
| Auth | XXXXXXXXXXXXXXXX | No Change | awscloudformation |
| Hosting | XXXXXXXXXXXXXXXX | No Change | awscloudformation |
| Storage | XXXXXXXXXXXXXXXX | No Change | awscloudformation |

Provide additional details e.g. code snippets

Error

✖ An error occurred when pushing the resources to the cloud

Attempting to edit the key schema of the ImageTable table in the Image stack.
An error occured during the push operation: Attempting to edit the key schema of the ImageTable table in the Image stack.

Updated Schema

// amplify/backend/api/chainappterparty/schema.graphql
type Image @model @key(fields: ["createdAt"]) {
  id: ID!
  long: String!
  lat: String!
  image: String!
  createdAt: String!
}

Original Schema

type Image @model {
  id: ID!
  long: String!
  lat: String!
  image: String!
}
graphql-transformer question

Most helpful comment

@kesteinbakk You're right - the primary index of a DDB table cannot be changed once the table is provisioned. Thw workaround would be a two step process:

  1. Remove the model (if you're fine with loosing the data stored in DDB)
  2. Add the model back with the desired @key attributes

All 2 comments

@alvincrespo: I think you are actually changing the primary index by adding an unnamed key with only one field. This will let you query on the "createdAt" field (insted of the ID field). If you add two fields, the first will be the query field and the second would be the sorting field of that query, for instance "@key(fields: ["id","createdAt"]). You can create a secondary index by giving it a name (keeping id as the primary): "@key(name: "byLong", fields: ["long","createdAt"]). See https://aws-amplify.github.io/docs/cli-toolchain/graphql

I came here because I actually wanted to change the primary index on a table, so I got the same issue you are describing.

Is it not allowed to change the primary index after the table is created???

@kesteinbakk You're right - the primary index of a DDB table cannot be changed once the table is provisioned. Thw workaround would be a two step process:

  1. Remove the model (if you're fine with loosing the data stored in DDB)
  2. Add the model back with the desired @key attributes
Was this page helpful?
0 / 5 - 0 ratings

Related issues

gabriel-wilkes picture gabriel-wilkes  Â·  3Comments

rehos picture rehos  Â·  3Comments

ReidWeb picture ReidWeb  Â·  3Comments

onlybakam picture onlybakam  Â·  3Comments

MageMasher picture MageMasher  Â·  3Comments