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

Cannot use key attribute in one DynamoDB index as non-key attribute in another index #4398

Closed
speller26 opened this issue Oct 7, 2019 · 4 comments · Fixed by #7075
Closed
Assignees
Labels
@aws-cdk/aws-dynamodb Related to Amazon DynamoDB bug This issue is a bug. needs-reproduction This issue needs reproduction. p1

Comments

@speller26
Copy link
Member

I'm using the CDK (in Python) to create global secondary indexes in my DynamoDB table, and one secondary index has a key that's used as a non-key attribute in another index. I have no problems doing this manually in the DynamoDB console, but when I use CDK, I get an error because CDK seems to check the union of key attributes across all indexes against the union of non-key attributes across all indexes. I traced the problem to this commit, specifically these lines:

Was this behavior intentional? This seems to restrict what one can do with CDK vs DynamoDB directly.

Reproduction Steps

In the following snippet, bar is used as a non-key attribute in Index1 and as a key attribute in Index2.

my_table.add_global_secondary_index(
    index_name = "Index1",
    partition_key = aws_dynamodb.Attribute(
        name = "foo",
        type = aws_dynamodb.AttributeType.STRING
    ),
    projection_type = aws_dynamodb.ProjectionType.INCLUDE,
    non_key_attributes = ["bar"] 
)
my_table.add_global_secondary_index(
    index_name = "Index2",
    partition_key = aws_dynamodb.Attribute(
        name = "baz",
        type = aws_dynamodb.AttributeType.STRING
    ),
    sort_key = aws_dynamodb.Attribute(
        name = "bar",
        type = aws_dynamodb.AttributeType.STRING
    ),
    projection_type = aws_dynamodb.ProjectionType.INCLUDE,
    non_key_attributes = ["blah"] 
)

Error Log

Error: a key attribute, bar, is part of a list of non-key attributes, bar,blah, which is not allowed since all key attributes are added automatically and this configuration causes stack creation failure

Environment

  • CLI Version :
  • Framework Version:
  • OS : Linux
  • Language : Python

Other

This could probably be fixed by changing the addGlobalSecondaryIndex method in Table so the key and non-key attributes are only compared per index.


This is 🐛 Bug Report

@speller26 speller26 added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Oct 7, 2019
@NGL321 NGL321 added @aws-cdk/aws-dynamodb Related to Amazon DynamoDB needs-reproduction This issue needs reproduction. language/python Related to Python bindings and removed needs-triage This issue or PR still needs to be triaged. labels Oct 7, 2019
@skinny85 skinny85 added the p1 label Oct 31, 2019
@ghost
Copy link

ghost commented Nov 7, 2019

I'm hitting the same issue in Java

@ghost
Copy link

ghost commented Nov 8, 2019

I have a workaround for Java (Kotlin, actually...), in case it's helpful to others:

table.addGSI {
    indexName("index-1")
    partitionKey(key1)
    sortKey(key2)
    projectionType(ProjectionType.INCLUDE)
    nonKeyAttributes(emptyList()) // we'll add these later to workaround a bug in CDK
}

table.addGSI {
    indexName("index-2")
    partitionKey(key3)
    sortKey(key4)
    projectionType(ProjectionType.INCLUDE)
    nonKeyAttributes(emptyList()) // we'll add these later to workaround a bug in CDK
}

// hack our way around this bug in CDK: https://github.com/aws/aws-cdk/issues/4398
val cfnTable = table.node.defaultChild as CfnTable
cfnTable.addPropertyOverride("GlobalSecondaryIndexes.0.Projection.NonKeyAttributes", listOf(
    "key3", "key4"
))
cfnTable.addPropertyOverride("GlobalSecondaryIndexes.1.Projection.NonKeyAttributes", listOf(
    "key1", "key2"
))

@cursive-ide
Copy link

I'm also hitting this bug using Typescript.

@skinny85 skinny85 assigned RomainMuller and unassigned skinny85 Mar 19, 2020
@RomainMuller RomainMuller removed the language/python Related to Python bindings label Mar 23, 2020
@jokoso
Copy link

jokoso commented Mar 29, 2020

I'm facing this issue as well. Thankfully, @perihelion1's work-around helps me for now.

RomainMuller added a commit that referenced this issue Mar 30, 2020
Removes the restriction that a key attribute cannot be used as a non-key
attribute in a distinct secondary index. This was excessively
restricting what can be done, as new integration tests demonstrated that
there is no problem in deploying such a configuration.

Fixes #4398
@mergify mergify bot closed this as completed in #7075 Apr 1, 2020
mergify bot pushed a commit that referenced this issue Apr 1, 2020
…er (#7075)

Removes the restriction that a key attribute cannot be used as a non-key
attribute in a distinct secondary index. This was excessively
restricting what can be done, as new integration tests demonstrated that
there is no problem in deploying such a configuration.

Also, switches from `Array`s to `Set`s to represent the sets of names that
must be unique.

Fixes #4398
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
@aws-cdk/aws-dynamodb Related to Amazon DynamoDB bug This issue is a bug. needs-reproduction This issue needs reproduction. p1
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants