The subject asks how to search a GridPanel, but the code seems to be searching a table. Then you mention making views or reorganizing the database in some way.
It is not clear if you are asking (1) how to search a table, (2) how to search a GridPanel or (3) how to reorganize your database.
1. Searching a table
If you want to search a table, @jshaffstall gave you the correct answer: you can’t do join queries in Anvil, so you need to first do a query on one table and then use the list of rows returned by the first query for the second query. In this case I would first search the teams table, then use the returned list when searching the employees table.
I would also let the database server do that, not with a list comprehension like you did in your example.
Something like this (untested) example should work, and it should be much faster than your list comprehension:
teams = app_tables.teams.search(name=q.ilike(f'%{query}%'))
employees = app_tables.employees.search(
first_name=q.ilike(f'%{query}%'),
last_name=q.ilike(f'%{query}%'),
pay_grade=q.ilike(f'%{query}%'),
team=q.any_of(teams),
)
2. Searching a GridPanel
If you want to search a GridPanel, I’m going to make a few assumptions: the GridPanel contains a small number of rows that can be efficiently scanned with a list comprehension, and the objects inside items
are row objects of the employees
table.
If this is the case, then the list comprehension you show almost works, but the last line should be changed from query in x['team']
to query in x['team']['name']
3. Reorganizing the database
If the teams table has only the name
column, then I would get rid of it and make the team
column a text column rather than a linked column.
Creating a linked table is the correct approach when you have full access to the SQL world, but in Anvil you have a limited access to the tables, you don’t have access to constraints, triggers and join queries.
Whenever I can, I use simple object columns rather than linked tables. For example I have an app that manages projects with trucks with crates with panels. In a normalized database they would be (at least) 4 tables, I have all in one trucks
simple object column. In some projects the project['trucks']
value can exceed 200kb, but when I load a project I know that I need all its trucks, crates and panels, and I will never search one panel or one crate or one truck. I will only search for a project first, then I can dig as much as I like in memory.