I'm trying to map with more than one structs. First mapping contains common details need to be mapped to every item and then second mapping should be carried out according to the item type.

    pragma solidity ^0.4.11;
    contract MyContract {

     struct CommonDetails {
      string name; 
      uint32 group;
      mapping (string => SpecificDetails1) items;

     struct SpecificDetails1{
      uint32 capacity;
      string owner;

    struct SpecificDetails2{
     uint32 capacity;
     string owner;

    mapping (address => CommonDetails) commonMapping;
    mapping (string => address)specificMapping;

    function setName(string _name, uint32 _group){

     commonMapping[tx.origin] = CommonDetails(_name, _group);

      if (_group == 1){
       // add SpecificDetails1 along with existing common details
       // if _group == 2 then add SpecificDetails2

   function getName() constant returns(address, string, uint32) {

     return (tx.origin, commonMapping[tx.origin].name, 
     // also want SpecificDetails in getter function

Initially, common details should be mapped to every item and then according to the input (for the group), 2nd struct should be mapped.

I have tried to create an array within struct and then push the specific details to that array but no joy.

My question is how can we perform 2nd mapping to another struct that is based on the input getting from the initial mapping?

If this is not possible within the single contract, can we do this importing this contract function into another contract and then perform 2nd mapping? And also how to get the output of both mapping in getter function, also.

Any guidance would definitely be appreciated!

  • Why do you need 2 different structs containing the exact same properties? Apr 24, 2018 at 19:42
  • The first struct contains some common details that I want to attach to every item. With the second struct, I want to attach some specific details. Let's take the example of student where every student has some common details and then based on student's major subject, we may attach some additional details. I'm not sure if a mapping within struct will help in this regard? But I want to make it 'if condition' based as described above in the comments within the code.
    – Yahya
    Apr 24, 2018 at 19:50
  • and these properties will contain different values depending upon the item, in spite of having same properties names but values will be different. Here just for the example, I used the same property names, but of course, names can be different.
    – Yahya
    Apr 24, 2018 at 19:59
  • Can modifier help here?
    – Yahya
    Apr 25, 2018 at 10:42
  • If we use the "student and class" example, you are describing a many-to-many relationship. That would be traditionally represented in three tables, one about the students, one about the classes (these have the "common" fields, and one about the intersection between students and classes where things like enrollment date, grade, date completed, etc., would reside. This is normalized design. Does that sound like what you want to achieve? Apr 25, 2018 at 17:22


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.