Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Solidity: problem creating a struct containing mappings inside a mapping

This is my code where i am trying to create a struct containing two mappings and insert the structs into a mapping:

pragma solidity ^0.7.2;

contract Campaign {
    struct Usuario {
        string id;
        mapping(string => uint) debe;
        mapping(string => uint) leDebe;
        
    }
    
    Usuario[] public usuarios;
    uint numUsuarios;
    mapping(string => Usuario) public circulo;
    
    constructor () {
        
    }
    
    function usuarioPrueba(string memory id, string memory idDebe, uint valDebe, string memory idLeDebe, uint valLedebe) public {
        
        usuarios.push();
        Usuario storage newUsuario = usuarios[numUsuarios];
        numUsuarios++;
        newUsuario.id = id;
        newUsuario.debe[idDebe] = valDebe;
        newUsuario.leDebe[idLeDebe] = valLedebe;
        
        circulo[id] = newUsuario;
    }
   
}

but I am getting the following error at line 28 (circulo[id] = newUsuario;) on Remix:

TypeError: Types in storage containing (nested) mappings cannot be assigned to. circulo[id] = newUsuario;

Thank you so much for the help beforehand and I am sorry for my english, I am from Spain and if the solution its just to obvious, I am kind of new to solidity and smart contracts.

like image 904
juan angel trujillo jimenez Avatar asked Sep 18 '25 03:09

juan angel trujillo jimenez


1 Answers

Since v 0.7.0 you cannot assign structs containing nested mappings. What you can do instead is to create new instances like this one and then asign the values to the properties of the struct!

 Usuario storage newUsuario = circulo[id];
    numUsuarios++;
    newUsuario.id = id;
    newUsuario.debe[idDebe] = valDebe;
    newUsuario.leDebe[idLeDebe] = valLedebe;
like image 108
David Pinilla Avatar answered Sep 19 '25 23:09

David Pinilla