TransWikia.com

Is there way to retrieve auto incremented primary key after submission of transaction?

EOS.IO Asked by Shashi on August 20, 2021

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?

One Answer

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

Answered by conr2d on August 20, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP