Behaviour of deleted row from live objects

I’m not sure if this is a bug or expected behaviour.

If I have a live object proxy and delete a row.
Then if I check it’s len it has 1 fewer row - as expected…
But if I try to iterate over the live object then it still tries to iterates over the deleted row which gives an error…

Minimal example below

  • A writable data table with 10 rows.
  • use search() to get all the rows
  • Check its len - 10
  • Iterate… no problems.

Next:

  • delete the last row
  • Check its len - 9
  • Iterate… error - item deleted.

https://anvil.works/build#clone:HVCUMRXYWMFNN2GY=D2XON6M7KTW5VVCFBMPX35LN

Hi! Thanks for a really clear report.

This behaviour is due to the SearchIterator’s caching behaviour. It doesn’t access the server until you actually request data from the row object, at which point it sees that the data is no longer present. As a general rule, if you’re likely to delete a row from a table that you have an existing SearchIterator for, you should handle the anvil.tables.TableError that may occur:

    for row in self.live_table:
      try:
        print(row['title'])
      except anvil.tables.TableError as exc:
        pass

len() is not cached, so the number of rows is still reported accurately.

3 Likes

Thanks Shaun noted - I think I would have expected the behaviour if it returned len as being 10 but because in returned len as 9 I got confused… It’s as if it knows something has changed but doesn’t know how to change itself. :face_with_head_bandage: