I have a Dynamo table and I am trying to retrive all items that are equal to a string for the primary key and the sort key
So let's say I want to retrieve all items from table mystery that column step
which is the primary key is equal to "release"
and column country
which is the sort key is equal to "Canada"
However I keep getting the following error KeyConditionExpressions must only contain one condition per key
What am I doing wrong?
query_foo("mystery", country_index, step, "release", country, "Canada")
def query_foo(table_name, index, p_key, value, s_key, s_value, start_key = nil)
@client.query(
table_name: table_name,
index_name: index,
scan_index_forward: false,
key_condition_expression: "#{p_key} = :k AND #{s_key} = :s",
expression_attribute_values: {
':k' => value,
':s' => s_value
},
select: 'ALL_ATTRIBUTES',
exclusive_start_key: start_key
)
end
UPDATE My code is actually CORRECT. I was passing a wrong value for one of the function parameters.
CodePudding user response:
I tested the following code and it worked as expected on my machine:
query_foo("test_table", "test_index", step, "release", country, "Canada")
def query_foo(table_name, index, p_key, value, s_key, s_value, start_key = nil)
@client.query(
table_name: table_name,
index_name: index,
scan_index_forward: false,
key_condition_expression: "#{p_key} = :k AND #{s_key} = :s",
expression_attribute_values: {
':k' => value,
':s' => s_value
},
select: 'ALL_ATTRIBUTES',
exclusive_start_key: start_key
)
end
CodePudding user response:
you can use the filter_expression parameter instead of the key_condition_expression parameter, like this:
def query_foo(table_name, index, p_key, value, s_key, s_value, start_key = nil)
@client.query(
table_name: table_name,
index_name: index,
scan_index_forward: false,
key_condition_expression: "#{p_key} = :k",
filter_expression: "#{s_key} = :s",
expression_attribute_values: {
':k' => value,
':s' => s_value
},
select: 'ALL_ATTRIBUTES',
exclusive_start_key: start_key
)
end
This way, you can use the key_condition_expression parameter to specify the condition on the primary key, and the filter_expression parameter to specify the condition on the sort key.