On Monday, February 25, 2013 8:24:48 AM UTC+1, Toby Wilson wrote:
>
>
> Hi all,
>
> New to web development and web2py. Know a bit of Python.
>
> As a learning experience I’ve written a food tracking program that works 
> really well in Python and it just runs on my local PC. It just reads and 
> writes values into text files.
>
> Now I’m trying to transfer that into web2py as a next step learning 
> experience. I’m trying to figure out the database side of it (models).
>
> It seems what I’m dealing with is many-to-many relationships. I have a 
> list of meals and each meal has a list of associated ingredients. So one 
> ingredient can be in many meal and each meal has many ingredients.
>
> So essentially, what the program I've written can do is add 3 x “Meal 1” 
> to the quantity existing in the database for “Meal 1” and then after doing 
> that prompt the user with:
>
> “Do you want this to impact the level of the ingredient ‘carrot’?” Y/N
>
> And then:
>
> “Do you want this to impact the level of the ingredient ‘steak’? Y/N
>
> etc.
>
> And the program knows that “Meal 1” needs 3 carrots, so upon entering “Y” 
> to the first question, 3 carrots are automatically subtracted from the 
> stock level of carrots in the database.
>
> So as meals are added, the user is prompted with associated ingredients 
> and can choose to update or not update the level of that associated 
> ingredient.
>
> In the local Python program I’ve written, all of this is just done through 
> listing things in dictionaries and using functions to manipulate the data 
> and then read and write to text files. Now I want to use database tables 
> and I’m not sure how much of the association should be in the model and how 
> much (if any) should be in the controller (for example in the form of a 
> dictionary).
>
> I’ve read through the documentation on many to many relationships but 
> haven’t got a full grasp of it yet.
>
> I’ve coded a few different database structures but haven’t managed to get 
> it right yet.
>
> The way I've been thinking about it is, a table for meals, a table for 
> ingredients, and perhaps a table for associations? I've used "reference" to 
> get the drop down box associated, but I can't select more than ingredient. 
> I had a look at the Django admin login and it visually made this look a bit 
> easier - i.e. building the one to many relationship. How is this done in 
> web2py?
>
Depends on what you need to build upon that model. If you're tracking 
"stock" too, then an ingredient table with the current quantity stored 
seems fair, and a table with meals too. 
On the "association" part, for an app like that you don't need to 
over-normalize the schema with a table holding the associations only: I 
doubt the ingredient "carrot" needs to change one day into "watermelon". 
You can fill the "association" part having a column in the meals table that 
references multiple ingredients with the list: types ...

http://web2py.com/books/default/chapter/29/06#list:%3Ctype%3E,-and-contains

> Any help or advice would be much appreciated.
>
> Toby
>
> P.S. If I have a database file in web2py and open the file through the 
> web2py IDE and replace the contents with a totally different database 
> structure and save it, what happens? Does web2py just remove what was there 
> in the database and replace the database tables with the new ones or does 
> it manage it in a different way (i.e. only add new elements etc.)?
>
web2py only watches .table files inside the databases/ folder and your 
models. If you have 2 table defined in your models and in the db there are 
other 15 tables, web2py can access only the 2 you defined. 
Additionally, migrations are fired only if models aren't in sync with the 
.table files, there is no introspection whatsoever on the db side.
So, if you have a table x with 3 columns and you change the structure 
outside web2py, web2py will try to access the table as it's defined in your 
model. 
If column names don't match, etc etc etc, you'll get errors trying to 
access something that isn't there.
 

-- 

--- 
You received this message because you are subscribed to the Google Groups 
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to web2py+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to