0

I need to use a string as a id of table in my smart contract. Initially i found some answers that string can't be use as primary key. But i use string as a key in table as given below:

//@abi table tttab i64
struct tttab
{
    string tableid;
    vector<uint64_t> preowners;
    uint64_t primary_key() const { return eosio::string_to_name(tableid.c_str()); }

    EOSLIB_SERIALIZE(tttab, (tableid)(preowners))
};

typedef multi_index<N(tttab), tttab> __mytable;

this works fine for me. My question is : Is this a right approach to use string as a id?

1 Answer 1

1

eosio::name is uint64_t internally and encodes string with BASE32. If you are sure that your tableid will contain chars in range of [.1-5a-z] and its length won't exceed 12 (or 13th-char only can have [.1-5a-j]), you can use it as primary key. By the way, the size of string is determined by its length, so if your tableid are expected to be longer than 8 in general case, to set the type of tableid as eosio::name seems to be a better solution.

Your Answer

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

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