2

Say I have a StorageDoubleMap like this

pub type Users<T: Config> = StorageDoubleMap<_, Twox64Concat, Vec<u8>, Twox64Concat, Compact<u64>, Person<T::AccountId>,>;

where the key1 = Vec<u8>, key2 = Compact<u64>. For example I store it like <Users<T>>::insert(b"hello", 100, person);

What if I want to change key1 from hello to new_hello?

2 Answers 2

2

One thing you can do is first take a value from storage, removing it afterwards, and insert it again with the new key:

    let value = <Users<T>>::take(b"hello", 100).ok_or(Error::<T>::UserDoesntExist)?;
    /// Insert it
    <Users<T>>::insert(b"new_hello", 100, value);
1
  • Oh nice, take is clever, thanks! Commented Jan 4, 2023 at 23:59
1

https://paritytech.github.io/substrate/master/frame_support/storage/trait.IterableStorageDoubleMap.html#tymethod.drain_prefix

I think you could write something like this to mutate the entire prefix tree:

for (k, v) in X::drain_prefix(old_prefix) {
  X::insert(new_prefix, k, v);
}

But this might cost a lot of weight. Be careful with that.

1
  • Thank you, I didn't see this function. Commented Jan 4, 2023 at 23:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.