Christian Buchwald
Christian Buchwald

Reputation: 570

AWS Amplify create Global Secondary Index after DynamoDB table creation

I have a large schema with ~70 tables and many of them connected to each other(194 @connection directives) like this:

type table1 @model {
  id:ID!
  name: String!
  ...
  table2: table2 @connection
}

type table2 @model {
  id:ID!
  ....
}

This works fine. Now my data amount is steadily growing and I need to be able to query for results and sort them.

I've read several articles and found one giving me the advice to create a @key directive to generate a GSI with 2 fields so I can say "Filter the results according to my filter property, sort them by the field "name" and return the first 10 entries, the rest accessible via nextToken parameter"

So I tried to add a GSI like this:

type table1 @model 
@key(name: "byName", fields:["id","name"], queryField:"idByName"){
  id:ID!
  name: String!
  ...
  table2: table2 @connection
}

running

amplify push --minify

I receive the error

Attempting to add a local secondary index to the table1Table table in the table1 stack. Local secondary indexes must be created when the table is created.
An error occured during the push operation: Attempting to add a local secondary index to the table1Table table in the table1 stack.
Local secondary indexes must be created when the table is created.

Why does it create a LSI instead of a GSI? Are there any ways to add @key directives to the tables after they have been created and filled? There are so many datasets from different tables linked with each other so just setting up a new schema would take ages.

Billingmode is PAY_PER_REQUEST if this has some impact.

Any ideas how to proceed?

Thanks in advance!

Regards Christian

Upvotes: 5

Views: 2485

Answers (2)

Marc Marti
Marc Marti

Reputation: 41

This is happening because you are creating a query on the table's partition key with a different sort key - which is how you define an LSI. If you want to create a GSI, you would need to create a query that uses a different partition key than the table's partition key.

If you want to query the partition key with different sort keys, you can download your data, comment out the table (and any references to it), amplify push, uncomment out your table and add the LSI's, amplify push, and upload your data back into it.

Upvotes: 2

Xin
Xin

Reputation: 36520

If you are using new environment, delete folder #current-cloud-backend first.

Then amplify init created the folder again but alas, with only one file in it amplify-meta.json.

Upvotes: 1

Related Questions