.netpaginationamazon-dynamodbrepository

Error: The provided starting key is invalid


im trying to paginate the checked in users in my dynamodb table, i have a GSI2PK and GSI2SK, the first is the event id of the user and the second is bool = true plus the checked in date, this is my repo function. When i get all the data without specifying the exclusive start key it works correctly, the issue is when i add an exclusive start key which in my case is the datetime as a string with no spaces.

        public async Task<(IEnumerable<UserManagementTable> Visitors, bool HasMore)> GetCheckedInVisitorsPaginatedByEventId(
            string eventId, string? exclusiveStartCheckInDate = null, int? limit = null, bool scanIndexForward = false)
        {
            var keyConditionExpression = "GSI2PK = :eventId AND begins_with(GSI2SK, :isCheckedInPrefix)";
            var expressionAttributeValues = new Dictionary<string, AttributeValue>
            {
                { ":eventId", new AttributeValue(eventId) },
                { ":isCheckedInPrefix", new AttributeValue("TRUE#") }
            };

            Dictionary<string, AttributeValue>? exclusiveStartKey = null;
            if (!string.IsNullOrEmpty(exclusiveStartCheckInDate))
            {
                exclusiveStartKey = new Dictionary<string, AttributeValue>
                {
                    { "GSI2PK", new AttributeValue(eventId) },
                    { "GSI2SK", new AttributeValue($"TRUE#{exclusiveStartCheckInDate}") }
                };
            }

            var query = new QueryRequest
            {
                TableName = "user-management",
                IndexName = "GSI2",
                KeyConditionExpression = keyConditionExpression,
                ExpressionAttributeValues = expressionAttributeValues,
                ScanIndexForward = scanIndexForward,
                ExclusiveStartKey = exclusiveStartKey,
                Limit = limit ?? 100
            };

            var result = await _dynamoDBClient.QueryAsync(query);

            var data = _context.FromDocuments<UserManagementTable>(result.Items.Select(Document.FromAttributeMap));

            var hasMore = result.LastEvaluatedKey != null && result.LastEvaluatedKey.Any();

            return (data, hasMore);
        }

tried to remove all spaces from the datetime as i thought that was issue but it still persists


Solution

  • You're simply not using the correct ExclusiveStartKey. As you're querying an index, the ExclusiveStartKey should contain the base table primary key also, which you fail to provide.

    I'm not sure on your pagination logic or what it is you're trying to achieve, but as a tool of thumb you should be passing the LastEvaluatedKey from the previous response directly to the ExclusiveStartKey.