I am trying to use the record = app_tables.clockinoutsystem.search(tables.order_by(‘Clock_In_Start’, ascending=False))[0] to get the last row from my data table. The problem is that this app is suppose to be multiuser so if two people start clocking in/out at the same time, it save the timestamp correctly. How would I be able to to use the app_tables.clockinoutsystem.search for multiple categories. What I want to do is to search for the last record of that particular user.
In table clockinoutsystem, do you have a column to identify the currently-logged-in user? You’ll need something like that to distinguish one user’s clock-out from another user’s.
Does this look about right for what I am trying to do?
record = app_tables.clockinoutsystem.search(tables.order_by('Clock_In_Start', ascending=False))[0] and app_tables.clockinoutsystem.search(Users=anvil.users.get_user())
First, let’s break it down and format it for readability:
iterator_over_all_my_records = app_tables.clockinoutsystem.search(
Users=anvil.users.get_user()
)
most_recent_record_from_some_user = app_tables.clockinoutsystem.search(
tables.order_by('Clock_In_Start', ascending=False)
)[0]
record = most_recent_record_from_some_user and iterator_over_all_my_records
The short answer is “no”. Consider the type of each of the two values being handed to Python’s and, and how Python’s and actually works. What you get won’t be a database table row, much less the row you want.
We need to pack all the criteria into a singlesearch. That is, we need to combine criteria, insidesearch’s parameter list. That gives the database server all the criteria at once. This way, it can make sure that the rows it returns meet all the criteria at the same time.
When you visited Searching (querying) a Table , you probably stopped well short of the example that shows you how to combine query conditions.
Edit 1: tell your browser to search that documentation page for Combining.
This has to be correct hehe. It supposes to search for both criteria in a single search (If I did it correctly), however, I don’t know how to reference a specific column to update afterward.
basically, after I find that row, I need it to update a column of that row to a specific date. I have no clue as to how to update it. I would think record[‘column name’] would work but it does not.
record = app_tables.clockinoutsystem.search(q.all_of(Users=self.user_name.text,Clock_In_Start=clock))
You don’t need q.all_of (it doesn’t hurt, but is doing nothing extra for you). The search function itself supports multiple query criteria by passing multiple arguments.
It’s probably a good idea to limit all Client-side table access to the rows owned by the current user. For help with that, look up Views, in the documentation. Specifically, Client-Readable and Client-Writable views. While there, also skim the Security model.
How would you have the same parameter more than once? I’m currently getting an error if the same field is added to the search function multiple times. The error is “syntax error keyword argument repeated”.
My example:
app_tables.mytbl.search(tables.order_by(“average_price”, ascending=True), average_price=q.less_than(high), average_price=q.greater_than(low))