2

I want to save name in table, and to search by it. It doesn't seems that it is possible to have secondary index with type name for some reason. I would like also to pass name in function and to have require_auth() on that name parameter. So the question would be, can i somehow convert that name to uint64_t so I can store it and have index on it?

Cheers,

1 Answer 1

3

This is the functionality that the multi index was based around.

To get the name, simple add .value to your key on return:

uint64_t primary_key() const { return key.value;} // key.VALUE

In the tutorial, they actually use a uint64_t for the secondary index. That's all eosio::name is under the hood, a uint64_t. Granted they use it to represent an age by a number, but it's still the same type nonetheless.

https://developers.eos.io/eosio-home/docs/secondary-indices

uint64_t age;
uint64_t get_secondary_1() const { return age;} // secondary index

typedef eosio::multi_index<"people"_n, person, 
indexed_by<"byage"_n, const_mem_fun<person, uint64_t, &person::get_secondary_1>>
  > address_index;

In this tutorial, you can see the primary index is indeed an eosio::name type:

struct person {
 name key;
 std::string first_name;
 std::string last_name;
 std::string street;
 std::string city;
 std::string state;

 uint64_t primary_key() const { return key.value;}
};
2
  • 1
    i was missing this "conversion" between name and uint64_t, by using .value! Thanks for pointing me to right direction
    – Bida
    Dec 18, 2018 at 14:27
  • Oh yeah, I should have added that. Thank you for reminding me!
    – Nat
    Dec 18, 2018 at 14:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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