Updating multiple rows in Data Table

I have a data grid in which the user selects multiple columns. The data grid is mapped to a table where each row in the grid is a row in the Data Table.

When the user selects multiple rows and submits the form, I want to update the Status of all selected items to the same status.

So far, I can only think of taking the selected list of objects and running an individual update() on the object to update to the new status.

Is there a better or faster way to process all selected objects in a single call or transaction? (similar to what you can do with a SQL statement?)

Thanks

Hi @singh,

You can perform Data Table operations in a transaction. This ensures your Data Table operations are carried out as a group.

You do this by decorating your server function with @anvil.tables.in_transaction. You can read more in the docs here:

Anvil Docs | Transactions

4 Likes

Thanks @bridget

So to clarify, if I have 5 rows (objects) where I need to update the Status column, will the decorated function hit the database 5 times, in a transaction, or does it batch up all 5 updates in a single call to the db.

Thanks

It will be 5 different updates inside the same transaction.

This is not a problem if you execute the 5 updates in one server side function, it is very slow if you do it from the client side.

It could actually be even more than 5 sql commands if you update more than one field per row. Here are two ways to do the same, the first does it in 3 commands, the second in 1:

row['col1'] = 1
row['col2'] = 2
row['col3'] = 3

row.update(col1=1, col2=2, col3=3)

The second is equivalent to:

row.update(**{'col1':1, 'col2':2, 'col3':3})
2 Likes