Home > other >  SQLAlchemy associating only one column with ID
SQLAlchemy associating only one column with ID

Time:08-16

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])
            db.session.commit()

CodePudding user response:

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

data = Ingredients(ingredients = getIngredient, amount = getAmount, unit = getUnit)
db.session.add(data)
db.session.commit()
  • Related