SQLAlchemy associating only one column with ID


I’m trying to get a value for ingredients, amount, and unit associated to a single ingredient_id. The individual columns get associated to a separate ingredient_id for some reason. Here is the output after trying to add it to the db.

ingredient_id ingredients amount unit
1 Ingredient1 Null Null
2 Null 1.0 Null
3 Null Null Amount1

Here is the table setup

class recipeMaster(db.Model):
    __tablename__ = 'recipemaster'
    id = db.Column(db.Integer, primary_key=True)
    recipe_name = db.Column(db.String(50))
    ingredients = db.relationship('Ingredients')

class Ingredients(db.Model):
    ingredient_id = db.Column(db.Integer, primary_key=True)
    recipe_id = db.Column(db.Integer, db.ForeignKey('recipemaster.id'))
    ingredients = db.Column(db.String(50))
    amount = db.Column(db.Float)
    unit = db.Column(db.String(20))

And here is how I’m committing it to the db. I’ve tried to add a single column at a time but get the same result.

ingredientName = Ingredients(ingredients = getIngredient)
            amountName = Ingredients(amount = getAmount)
            unitName = Ingredients(unit = getUnit)
            db.session.add_all([ingredientName, amountName, unitName])


Tried committing it like this and got the output I expected.

data = Ingredients(ingredients = getIngredient, amount = getAmount, unit = getUnit)

Answered By – spoon

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published