amplify-cli: Bug with amplify generated composite keys
Hello, I created a composite sort key using the amplify cli. The composite key includes the a person’s first and last name. Most of the time the composite key is generated correctly in Dynamo DB like: john#doe. However, sometimes it’ll be saved as ${ctx.args.input.first}#${ctx.args.input.last}. Subsequently, when I try to run a query with this composite key, it skips over the records that have this faulty composite key. I have not messed with any of the code auto generated by amplify and have confirmed that the first and last name values due indeed exist in the data row. Also, the primary keys were also defined. If anyone can point me in the right direction that would be great!
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 10
- Comments: 22 (5 by maintainers)
@attilah @kaustavghosh06 @SwaySway @yuth @ammarkarachi @edwardfoyle @litwicki this is such a critical bug it should be at the highest priority of any work for Amplify CLI. This essentially makes accessing DynamoDB data that uses a composite sort key completely unreliable and inconsistent because a record “disappears” (is not returned) if an update is performed that does not include the fields that make up the composite sort key. This can cause customers to think their data just disappeared… how can anything be more critical to fix than that? This has already been well diagnosed. Can the Amplify community please have an update when this will be fixed?
any updates? still a bug after being identified over a year ago (v.4.49.0)
@yuth Just ran into same issue. This happens in my case if in a mutation to update the record, the fields that constitute the composite key are not included in the input. Manually changing like below solves the issue.
#set( $hasSeenSomeKeyArg = false ) #set( $hasSeenAllKeyArg = true ) #set( $keyFieldNames = ["status", "fromDate"] ) #foreach( $keyFieldName in $keyFieldNames ) #if( $ctx.args.input.containsKey("$keyFieldName") ) #set( $hasSeenSomeKeyArg = true ) #else #set( $hasSeenAllKeyArg = false ) #end #end #foreach( $keyFieldName in $keyFieldNames ) #if( $hasSeenSomeKeyArg && !$ctx.args.input.containsKey("$keyFieldName") ) $util.error("When updating any part of the composite sort key for @key 'byCustomer', you must provide all fields for the key. Missing key: '$keyFieldName'.") #end #end ## [End] Validate update mutation for @key 'byCustomer'. ** #if( $hasSeenAllKeyArg ) #if( $util.isNull($dynamodbNameOverrideMap) ) #set( $dynamodbNameOverrideMap = { "status#fromDate": "statusFromDate" } ) #else $util.qr($dynamodbNameOverrideMap.put("status#fromDate", "statusFromDate")) #end $util.qr($ctx.args.input.put("status#fromDate","${ctx.args.input.status}#${ctx.args.input.fromDate}")) #endThat way composite key will be updated only of all fields available (not sure if this is actually a behaviour you want, or if it leads to logical issues)