What I’m trying to do:
I am trying to link a table named ‘litters’ to a column in a data table named ‘dams’ the column is called ‘LitterLink’ What I’ve tried and what’s not working:
I have tried using the following code. The code runs perfectly fine with no errors, but when I look at the data table I am trying to put the links in, the column remains to be ‘None’ and contains no links whatsoever!
@anvil.server.callable
def linking():
# Get the rows from the 'dams' table
allDams = app_tables.dams.search()
# Iterate over each row in the table
for row in allDams:
damName = row['Name']
# Print the name of the current row
print(f"Processing row with name: {damName}")
# Search for matching rows in litters table
litterRow = app_tables.litters.search(Dam=damName)
# Get a list of matching litter rows
matchingRows = [litterRow for litterRow in litterRow]
# Print the matching rows
print(f"Matching rows: {matchingRows}")
# Link the list of matching 'litters' talbe rows to the 'dams' table
row['LitterLink'] = matchingRows
# Update the individual row in the 'dams' table
row.update()
Console Output:
Processing row with name: hgu
Matching rows:
Processing row with name: test
Matching rows: [<LiveObject: anvil.tables.Row>, <LiveObject: anvil.tables.Row>]
Processing row with name: uygo
Matching rows:
You are right that it will be simpler but that still doesn’t fix my problem of it not adding it to the data table in the first place. (also thanks for letting me know about my typo)
NVM I HAVE FOUND THE SOLUTION. The data tables do not update once the app has finished running UNLESS you close the data table, then run the app, then open it again. Tbh my mistake but il just keep this as the solution so that other people don’t run into the same problem.
No, this is not the solution. If you keep the code shown in your question, it will never work, no matter what you do with the IDE.
The only way to get your code to update the datatable is to fix the list comprehension the way I said in my earlier post.
Your only mistake was to rely on the UI to check the values in the table assuming that the UI wouldn’t change the values without telling you. It it is a very understandable mistake, caused by a problem in the UI design, and you are not the first one to fall for it. I created this feature request addressing it some time ago, I hope it will be addressed soon, and you can see that other posts are linking to this one, so you and me are not the only ones that found this problem.
So, yeah, you had a problem in the code and you were assuming that the UI would allow you to check the values stored in the table, but the UI was actually changing those values and preventing you from noticing the error in the code.