nodecosmos / charybdis

Rust ORM for ScyllaDB and Apache Cassandra
MIT License
108 stars 6 forks source link

Update field using GSI #25

Closed Zk2u closed 2 months ago

Zk2u commented 2 months ago

Hiya,

Thanks for all your work as usual. I'm trying to update a field on a row to be true by using the GSI to lookup the row. I can't use the partial macro as it requires the primary key. How would I do this?

GoranBrkuljan commented 2 months ago

That is limitation of Scylla itself. You can not update records by their secondary index, instead you would need to read them first and then update.

In case you need to update more then one record you would usually use batch statements:

use charybdis::batch::ModelBatch;

// find and collect users
let mut users: Vec<User> = User::find_by_gsi(gsi)
    .execute(db_session)
    .await?
    .try_collect()
    .await?;

users.iter_mut().for_each(|user| {
    // updates
    user.text_field = "new_value".to_string();
});

User::batch().append_updates(&users).execute(db_session).await?;

In case your batch is too big, you could use chunked_updates:

// update 100 by 100
User::batch().chunked_update(db_session, &users, 100).await?;
Zk2u commented 2 months ago

I think I can find the primary keys using the GSI then do the update through the primary keys. Thanks for the help :)