Hi there,
Is there any way to get table name from datatable row object. Something like this
def function_a(datatable_row):
return (tablename(datatable_row))
Thank you,
Hi there,
Is there any way to get table name from datatable row object. Something like this
def function_a(datatable_row):
return (tablename(datatable_row))
Thank you,
Hi @mussa.butt,
There’s no built-in way to get the table name from the Row object.
Are you able to provide a bit more context about what you’re doing? I’d have thought that usually if you’ve retrieved a Data Table row, you know what table it came from, but of course I haven’t thought of all possible use-cases!
If you can narrow it down to a finite list of tables you could try something like this:
def table_of_row(row):
for table in [app_tables.table1, app_tables.table2]:
if table.has_row(row):
return table
raise Exception("Row was not in list of tables.")
That still runs a lookup for the row in each table, which may require round-trips to the database, depending on caching. A better solution would be to check that the candidate Row has the correct columns - assuming you don’t have two tables with identical schemas:
def table_of_row_faster(row):
for table in [app_tables.table1, app_tables.table2]:
columns = [c['name'] for c in table.list_columns()]
for column in columns:
try:
row[column]
except IndexError:
# This column was not in the row, so it's not from this table
break
else:
# We didn't break out of the loop, so this must be the correct table
return table
raise Exception("Row was not in list of tables.")
Here’s an app that demonstrates that
https://anvil.works/build#clone:EIRJ6TILVXXVIZVE=7XFABDB7H7HEDCXCVXJ6DUM3