Hi there, wondering how one can delete one record in a mapping of mapping? Got an error like this "TypeError: Unary operator delete cannot be applied to type mapping(uint16 => string storage ref)" in the following operation:

Apr 24, 2021, 10:56 AM
mapping(uint256 => mapping(uint256 => string)) _CIDs;
delete _CIDs[someId];
Sort of like deleting the allowances of a user when it is deleted in a ERC20 token.
Apr 24, 2021, 10:59 AM
You cannot delete mappings as they always have a value. You can set the string to "" and later check that or replace the mapping by a new entry
Apr 24, 2021, 11:37 AM
Thanks. That is what I figured. It would still require iterating through the second mapping.
Apr 24, 2021, 11:41 AM
You can't delete a record in blockchain. Whatever your use case may be, a record you intend to delete, maybe just set a status of "deleted".
Apr 24, 2021, 11:56 AM
Yes, it's not the literal meaning of deleting. It is really about erasing the values at that piece of memory such that further reading at that location would return no meaningful value. The question was how one could do it effectively.
That said, I'm wondering if new values are always written to new locations. The concept of overwriting does not exist for a blockchain, is that true?
For example, for this: mapping(uint256 => string) _aMap; _aMap(someId) = "an old string"; _aMap(someId) = "an new string";
The second operation does not overwrite the first key-value pair, but rather create a new pair on storage, is that true? (This is deep. Just thinking loud. Not expecting an answer).
Apr 24, 2021, 12:06 PM
yes this will be a new transaction. There's really no other way that I can think off because either way, you will be writing on the ledger.
Apr 24, 2021, 1:07 PM

© 2024 Draquery.com All rights reserved.