General Question
Assumptions
emails for bob, sally, and Joe are located in a single text column, separated with commas, in a data table called users.
We then have user bob log in with his email and that email user email is = to label.text. Using the label as your reference, how do you iterate through that row of multiple users to see if the user is in the list?
I tried a few things, and many tips on linking data tables together, but I still can’t imagine how I can check through this.
You’re actually asking a pretty specific question, with no context given. If you want advice on getting linking tables to work, or on best practices for designing data tables for a given situation, you should provide a lot more context.
In general, though:
You wouldn’t setup your data table that way. If you know it’s going to be a list of emails, make it a simple object column and store a list of strings in it. Then you’re dealing with a list, and use the standard Python in keyword to search it.
A simple object column also works well with query operators, so you might be able to let the database do the testing for you.
Sorry @jshaffstall , I say general question as I do not have a specific example with code. Trying to rethink this project of mine.
How does the simple object column separate each email? assuming the data was entered this way. bob@gmail.com, mike@gmail.com into a textbox, or more specifically a text area? How does it know that each email is different? What steps do I need to take to indicate where it separates? Maybe I am not asking the right question, I just to understand the details as much as possible before I get started.
Your simple object column would hold a Python list. So you would take your string from the text box and convert it to a list of strings using standard Python techniques. That list of strings would go into your simple object column.
If you’re not sure what the standard Python techniques are for converting a string to a list, that’s something you can easily Google and come up with the normal approach.
At this point in time (more experience), I think I chose to do this in the most DIFFICULT way possible. Can you describe the data organization method you recommended? How would you link them to a single staff owner? A copy of an app to see the data structure can really give me some tips on it.
@jshaffstall So I got my string and turned it into a list. Super easy
How do I structure the data so I can insert it into the simple objects column? I received a syntax error.
So, all you really need to do is have a table called, for example, company. In there could you have columns like Company, URN, Staff and Team.
If you then had a second table called reps, in there you could have columns called URN, Team and Reps, where you put the email address list as a SimpleObject.
This is without going into the witchcraft of Linked Tables, but it gives you a whole range of mix and match search capability to experiment with e.g.
Company = self.input.text
URN = app_tables.company.get(Company=Company)['URN']
Team = self.input2.text
Staff = [row['Staff'] for row in app_tables.company.search(Company=Company,Team=Team)]
for reports in Staff:
Reps = app_tables.reps.get(URN=URN,Team=Team)['Reps']
Not supposed to be perfect, but is supposed to give you some generalised ideas to flesh out by playing.
Don’t feel too bad about that. Designing database tables, to fill a need, is a discipline unto itself, with roots going back 50 years.
That said, there are a few sound design principles, that still apply to Anvil tables. I’ll suggest a few of them here.
Think of a table as a “set of things”, where all the things are of the same general kind or type.
Each row will describe one of the things.
The columns list out the details you want [the database] to remember about each row. Some details might be optional. Sometimes, this takes several columns working together.
In Anvil, as in most databases, columns are strongly typed. All the values in a column have the same type:
A cross-reference, to one or more related rows, in this table, or in a different table.
A number
A string
A “flag” (True/False) value
A date, time, or both
A structured set of data (“Simple Object”); generally, a Python list or dict.
etc.
If a “thing” is composed of or contains other “things”, then it’s usually a good idea to give those sub-things their own table.
In that table, each row can refer back to its “container” or “owner”.
If you find yourself cramming lists of things into a column, remember point number 2 above.
A good place to start, for structuring and organizing your tables, might be here:
@p.colbert I learned about databases and data tables from designing apps in Appsheet, which is where I learned how to use databases (in my case google sheets), but I love the history. One of the good things about app sheets is that it is pretty easy to rationalize your databases.
I know some of the concepts, but I am having a hard time coding it and comparing them Anvil. You all have been a great help in teaching/helping me figure out how to code.
There’s a huge diversity in experience in this community. Really quite a powerful knowledge base and always happy to help too. So, like the rest of us, you get the best framework to develop with AND get to lean on good people. What’s not to love?
@james.patrick You guys are one of the best communities out there to learn for newbies. Concepts I understand, since Ive taken basic classes on udemy/sololearn but to truly understand python and how it works with Anvil you need a community. I want to become as proficient in Anvil as possible. Once I feel comfortable, I want to create tutorial videos to help explain concepts and do walk-throughs. Anvil is one of the best tools I’ve used thus far.