简体   繁体   中英

Composite Primary Key - Dynamodb for Rust

How does one define a composite key in dynamodb in rust? Currently we are creating our table similar to the rust CRUD github official example

    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);
        }
    };

In python and JS you can pass in a dictionary object to.key_scheme and.attributes, but in the Rust implementation it seems that key_scheme is defined as

        pub fn key_schema(mut self, input: crate::model::KeySchemaElement) -> Self {
            self.inner = self.inner.key_schema(input);
            self
        }

So we're not sure how to create a composite primary key!

Going off the documentation it appears you would call .key_schema multiple times for a composite key. The docs say:

Appends an item to KeySchema .

To override the contents of this collection use set_key_schema .

[...]

For a composite primary key (partition key and sort key), you must provide exactly two elements, in this order: The first element must have a KeyType of HASH , and the second element must have a KeyType of RANGE .

So it appears your definition should look like:

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]))

And a similar pattern is documented for attribute_definitions and set_attribute_definitions . Disclaimer: I'm not very familiar with DynamoDB, I'm just relaying the documentation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM