I have finally found a way to insert, update and delete ingredients from 
the recipe application.
It is probably not the best way but it works.
First since I had a button in the form to add an ingredient, I used a 
button to delete a selected ingredient.

As for the Update and insert into an existing recipe placed the entire 
recipe into a dictionary of dictionaries

Data = {'Recipe': {'recipeName': 'Test', 'recipeDescription': '1. Mix 
everything\n2. Cook\n3. Turn off Stove', 'recipeCategory': 2, 'recipeKey': 
33}, 'Ingredients': {'update': [['133', 'Item1', 33, '1'], ['134', 'Item2', 
33, '6'], ['136', 'Item4', 33, '4']], 'insert': [[None, 'Item5', 33, '5']]}}

The 'Recipe' key updates the Recipe table and the 'Ingredients' updates (if 
necessary inserts) ingredient data into the Ingredient Table

The code:

def updateRecipe(data):
    session = connectToDatabase()
    
    primeKey = data['Recipe']['recipeKey']
    
    # Update the recipe table
    recipeResult = 
session.query(Recipe).filter(Recipe.recipeKey==primeKey).one()
   
    recipeResult.recipeName = data['Recipe']['recipeName']
    recipeResult.recipeCategory = data['Recipe']['recipeCategory']
    recipeResult.recipeDescription = data['Recipe']['recipeDescription']
    
    
    # update the ingredients table
    IngredData = data.pop('Ingredients')
    ingredientResult = 
session.query(Ingredients).filter(Ingredients.ingredientRecipeKey==primeKey)
    for Ingred in ingredientResult:
        for item in IngredData['update']:
            if int(Ingred.ingredientKey) == int(item[0]):
                Ingred.ingredientDescription = item[1]
                #Ingred.ingredientRecipeKey = item[2]
                Ingred.ingredientQuantity = item[3]
                session.add(Ingred)
                
    # insert New Ingredients:
    key = 'insert'
    if key in IngredData.keys():
        for item in IngredData['insert']:
            
recipeResult.tblIngredients.append(Ingredients(ingredientDescription=item[1],
                                                ingredientQuantity=item[3]))
    session.add(recipeResult)
    session.commit()
    session.close()


-- 
SQLAlchemy - 
The Python SQL Toolkit and Object Relational Mapper

http://www.sqlalchemy.org/

To post example code, please provide an MCVE: Minimal, Complete, and Verifiable 
Example.  See  http://stackoverflow.com/help/mcve for a full description.
--- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sqlalchemy/d5d302f4-d03b-45b0-b7fb-1c9b40cb933dn%40googlegroups.com.

Reply via email to