Support Topics Documentation Slack YouTube Blog

Documentation for UnitOfWork and Transactions for Flutter on Backendless (v.6.0.0)

Hi,
I need to create a table and then use the id as a foreign key in the creation of several other tables in a single transaction.
I see you have a UnitOfWorkCreate class and number of other classes that might be of assistance but I can’t find any documentation on how to use them. What is the best way to achieve this? Also I need to store the foreign key in another column instead of the ownerId column.

Any help would be greatly appreciated.
Thanks

Hello,

Do I understand correctly that you want to create records in different tables based on records from another table?

Regards,
Olha

Hi Olha,

Yes. That’s correct, but I need to do all this work within a single transaction.
I can’t find any documentation for setting things like OperationType or UnitOfWorkCreate.
Is there some other way we should be doing this?

I want to:

===============

Start a transaction:

Then create a new record in Table A

Get the objectId from the new record

Then I need to create Table: B, C, D, E (I need the new record ID from Table A to use for specific columns in B, C, D, E.

Then complete the transaction as successful or rollback if error.

==============
Thanks,

Hi @Carlton_Branch

Here is the documentation for Transactions:

Take a look at “Saving a single object” and “Setting a relation” chapters.

The code should be like this:

final uow = UnitOfWork();
final recordA = {
 "foo": "bar",
};
final aRecordOpResult = uow.create(recordA, "TableA");
...
final bRecordOpResult = uow.create(recordB, "TableB");

unitOfWork.setRelation(
   bRecordOpResult,
   relationColumnName,
   aRecordOpResult,
   "TableB");

Best Regards,
Maksym

Hi Maksym,
So if I understand correctly, the bRecordOpResult on a create will return the id of the record created?

The bRecordOpResult is the Operation Result. From the doc:

Operation Result - an entity representing the result of an individual database operation. The result of one operation can be referenced in other subsequent operations. You can see an example of it in the sample code shown above. For example, a “create” operation returns a result. The result represents the object saved in the database. That object may be referenced in an operation to set a relationship for the object. Alternatively, you can “extract” a value of a specific property from the operation result and also use it in other operations in the same transaction.

You cannot get the id of the objects because they haven’t been saved on the server yet. That is why the OpResult exists. It allows you to reference the objects from one operation in the another one.

1 Like

Hi,
I just need an answer to this question:

In a single transaction: how can I get the objectId from one Create Transaction (create) to create or update another record in a different table?

The example you provided does not match the documentation and it also doesn’t work for me. So I just want to know how I can get the objectId and use it in another record or table. I don’t want to set any specific relation between the tables.

===================================
I just want to take the objectId from one Create Action and use it in another Create or Update action in the same Transaction.

===================================
Is this possible with Backendless or not? Right now, I am just getting “Instance of ‘OpResultValueReference’” stored in my database instead of the appropriate objectId.

  var stackCreateResult = uow.create(stack.toMap(), "Stack");
  var stackIdReference = stackCreateResult.resolveTo(propName: "objectId");

  for (var quebeDetailId in quebeDetailIdList)
    uow.create({
      'quebe_detail_id': "$quebeDetailId",
      'stack_id': "$stackIdReference"
    }, "Quebe");

Thanks,
Carlton

Hi @Carlton_Branch

There is a little spelling mistake in your code. The stackIdReference value shouldnt be wrapped in brackets. The code should be like this:

  var stackCreateResult = uow.create(stack.toMap(), "Stack");
  var stackIdReference = stackCreateResult.resolveTo(propName: "objectId");

  for (var quebeDetailId in quebeDetailIdList)
    uow.create({
      'quebe_detail_id': "$quebeDetailId",
      'stack_id': stackIdReference
    }, "Quebe");

Best Regards,
Maksym

Glad it works for you!
Happy coding with Backendless!