Get_by_row_id()

Continuing the discussion from Needed: A call something like app_tables.my_table.get_table_id():

It would also be amazing if there was a server function that could handle get a row from ANY table just by the ID.

row_from_id = app_tables.get_by_id(row_id)

Or something similar. This could obviously be built if we had the linked request but it would be nice if it was an inbuilt solution.

There is a get_by_id() function described in here :

Is that what you meant or have I misunderstood?

For that function to work you need to specify the table. What I am meaning is finding the table and the row just from the row ID

Ah I see, sorry. What format is the row ID at the moment? The id would have to be a UUID for that to stand a chance of working, and not an integer like (eg) MySQL uses for auto_increment.

App Table row IDs look like this "[123456,987654321]"
the first number is the table ID and the second number is the actual row. If you want to use app_tables.my_table.get_by_id("[123456,987654321]") you still need to know what my_table is.

What I am after is something that doesn’t need to know what the table is ahead of time, it can still get the row. This is particularly useful if are storing linked rows as Row IDs in offline indexed_db or JSON objects. You can still get your rows on the server with minimal fuss.

from anvil.tables import get_table_by_id

Exists, however:

Since it exists in the beta, I’m not sure if will ever be implemented in the “classic” version of anvil.tables

I do not have accelerated tables turned on on any of my apps, but you could test it like this:

from anvil.tables import get_table_by_id


@anvil.server.callable
def get_table_by_id_test1():
  row =  app_tables.your_table_here.search()[0]
  print(get_table_by_id(row.get_id()).list_columns())

Once you have the ability to get your table back as an object, you could put this all in one line with .get_by_id(row_id)

Edit:
aka

def get_by_row_id(row_id)
  #  Only works with Accelerated Tables turned on
  return anvil.tables.get_table_by_id(row_id).get_by_id(row_id)
4 Likes