Solidity: problem creating a struct containing mappings inside a mapping

5.8k views Asked by At

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.

1

There are 1 answers

0
David Pinilla On

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;