As per title, I'm using a key256 as a datatype for a secondary index on a multi-index table and want it to be unique. Can't find any documentation on it. TIA.


Yes you can, but it is optional.

From the docs:

the primary index type must be uint64_t and must be unique, but the other, secondary, indexes can have duplicates

Example also indicating that secondary index may be unique:

struct [[eosio::table]] poll 
  uint64_t key; // primary key
  uint64_t pollId; // second key, can be non-unique
  std::string pollName; // name of poll
  uint8_t pollStatus =0; // staus where 0 = closed, 1 = open, 2 = finished
  std::string option; // the item you can vote for
  uint32_t count =0; // the number of votes for each time

  uint64_t primary_key() const { return key; }
  uint64_t by_pollId() const {return pollId; }

The standard way of making this unique is in your creation of the row by adding the available_primary_key() method:

Returns An available (unused) primary key value.

_polls.emplace(get_self(), [&](auto& p) {
        p.key = _polls.available_primary_key(); // method
        p.pollId = _polls.available_primary_key();
        p.pollName = pollName;
        p.pollStatus = 0;
        p.option = "";
        p.count = 0;

Link to method in docs: https://developers.eos.io/eosio-cpp/reference#available_primary_key

|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.