1

Below I have created a table, the full_name, birth_year, nationality and ipfs_hash are all not unique values.

Do I need one of them to be unique? Much like a database can I ask for a field to "auto increment" or do I need to call the contact with a value that has a good chance of being unique? Do I need to make a field with 'key'?

...
private:
  struct [[eosio::table]] individual {
    std::string full_name;
    std::string birth_year;
    std::string nationality;
    std::string ipfs_hash;
    uint64_t primary_key() const { return key.value; }
  };

  typedef eosio::multi_index<"individuals"_n, individual> individual_index;
};
2

Each row must have a unique primary_key returned by individual::primary_key, or another row which has same primary key will not be added to table due to violation of primary key uniqueness. You can add new field for primary key like the next.

...
private:
  struct [[eosio::table]] individual {
    uint64_t key; // new field which contains unique primary key
    std::string full_name;
    std::string birth_year;
    std::string nationality;
    std::string ipfs_hash;
    uint64_t primary_key() const { return key; }
  };

  typedef eosio::multi_index<"individuals"_n, individual> individual_index;
};

{
  individual_index iid(_self, _self.value);
  iid.emplace(payer, [&](auto& i) {
    i.key = iid.available_primary_key();
    ...
  });
}

Or, if you don't like to add new field only for primary key, you can generate primary_key by your own logic.

...
private:
  struct [[eosio::table]] individual {
    std::string full_name;
    std::string birth_year;
    std::string nationality;
    std::string ipfs_hash;
    uint64_t primary_key() const { return SOME_64BIT_HASH_FUNC(full_name, ...); }
  };

  typedef eosio::multi_index<"individuals"_n, individual> individual_index;
};
| improve this answer | |

Your Answer

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

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