I'm working on sample dApp application to understand the working of multi_index table in EOS smart contract. I have defined two multi_index table. The primary key of table1 is auto incremented using "primary_key()". The table two has one column which refers to primary key of table1 (like foreign key in DB). I have a sequence where I first add an entry to table1 using eos-go and then need to create an entry in table2 with retrieving inserted primary_key in the table1. I don't find a way to fetch the auto incremented primary key after completing create transaction to table1.

Is there any way to fetch/return last inserted primary key like in sql in EOS smart contract?

1 Answer 1


There is an improvement that makes action can have return value, but not applied to mainnet yet. After its applying, you can check that value from action_receipt.

Current feasible solution is making a table storing the result of last executed action and reading it.

auto it = some_table_index.emplace(payer, [&](auto& row) {
   row.id = some_table_index.available_primary_key();
   /* ... */

// you can access last incremented key by "it->primary_key()"
// make independent multi_index table or singleton to store this value

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.