[英]Composite Primary Key - Dynamodb for Rust
如何在 rust 中的 dynamodb 中定義復合鍵? 目前我們正在創建類似於 rust CRUD github 官方示例的表
match client
.create_table()
.table_name(table_name)
.key_schema(ks)
.attribute_definitions(ad)
.provisioned_throughput(pt)
.send()
.await
{
Ok(_) => println!("Added table {} with key {}", table, key),
Err(e) => {
println!("Got an error creating table:");
println!("{}", e);
}
};
在 python 和 JS 中,您可以將字典 object 傳遞給.key_scheme 和.attributes,但在 Rust 實現中,key_scheme 似乎被定義為
pub fn key_schema(mut self, input: crate::model::KeySchemaElement) -> Self {
self.inner = self.inner.key_schema(input);
self
}
所以我們不確定如何創建復合主鍵!
從文檔中可以看出,您會多次調用.key_schema
來獲取復合鍵。 文檔說:
將項目附加到
KeySchema
。要覆蓋此集合的內容,請使用
set_key_schema
。[...]
對於復合主鍵(分區鍵和排序鍵),您必須按以下順序恰好提供兩個元素:第一個元素的
KeyType
必須為HASH
,第二個元素的KeyType
必須為RANGE
。
所以看起來你的定義應該是這樣的:
client
.create_table()
.table_name(table_name)
.key_schema(ks1)
.key_schema(ks2)
// or
client
.create_table()
.table_name(table_name)
.set_key_schema(Some(vec![ks1, ks2]))
並且為attribute_definitions
和set_attribute_definitions
記錄了類似的模式。 免責聲明:我對 DynamoDB 不是很熟悉,我只是轉發文檔。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.