【问题标题】:Flask remove items from another table烧瓶从另一个表中删除项目
【发布时间】:2021-05-08 15:47:01
【问题描述】:

我有一个包含食谱和配料的数据库:

class Recipe(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    ingredient = db.relationship('Ingredient', backref='ingredientrecipe', lazy=True)
    recipe_name = db.Column(db.String(100), nullable=False)

class Ingredient(db.Model):
    id = db.Column(db.Integer, primary_key=True) 
    recipe_id = db.Column(db.Integer, db.ForeignKey('recipe.id'), nullable=False)
    name = db.Column(db.String(100))

成分列在 html 表格中。 (为了清楚起见,我没有包含模态的完整 Bootstrap html,尽管它表现出一些奇怪的行为,尽管它具有唯一的 id)

<table class="table">
    <thead>
      <tr>
        <th scope="col">#</th>
        <th scope="col">Name</th>
      </tr>
    </thead>
    <tbody>
    {% for ingredient in ingredient.items %}
    <tr class="ingredient">
        <th scope="row"> {{ hop.id }} </th>
        <td>{{ ingredient.name }}</td>
        <td>
        <modal html here>
        <form action="{{ url_for('deleteingredient', ingredient_id=ingredient.id, recipe_id=ingredient.recipe_id) }}" method="POST">
        <input class="btn btn-danger" type="submit" value="Delete">
        </form>
        </modal html>
        {% endfor %}
    </tr>
    </tbody>
</table>

这是我的删除路线:

@app.route('/recipe/<int:ingredient_id>/delete', methods=['POST'])
@login_required
def deleteingredient(ingredient_id):
    recipe = Recipe.query.get_or_404(recipe_id)
    ingredient = Ingredient.query.filter_by(ingredientrecipe=recipe)
    if recipe.author != current_user:
        abort(403)
    db.session.delete(ingredient)
    db.session.commit()
    flash('Your ingredient has been deleted', 'success')

我不会发布当我尝试执行此操作时出现的错误消息,因为我认为它不相关(它是与 recipe 相关的不同表上的 (sqlite3.IntegrityError) NOT NULL constraint failed)并且可能会混淆问题。

想要的行为是转到单个食谱页面,单击成分列表中单行上的按钮,以弹出模式,并从数据库中删除单个成分行。

我很确定这是因为我没有理解数据库结构背后的逻辑以及路由如何与相关表一起工作。任何有助于减少我的脑损伤的帮助将不胜感激!

【问题讨论】:

    标签: flask flask-sqlalchemy


    【解决方案1】:

    通过查看您的代码,我可以发现一些可能破坏逻辑的问题:

    • 你永远不会在你的deleteingredient 函数中使用ingredient_id
    • recipe_id 未定义

    Recipe 模型中ingredient 关系的目的是什么?由于一个配方有多种成分,而一种成分可以有多种配方,因此这是一种 n-n 关系。也许你应该考虑看看this

    【讨论】:

    • 这种关系是一对多的——每种成分只存在于那个 1 配方(它是从成分主表中复制的)。所以一个食谱可以有多种成分,但不能反过来。
    • Re the recipe_id 的东西 - 谢谢,一旦我的大脑恢复正常,我会重新检查这个问题。今天工作太多了!
    • 感谢@Mathieu 的建议——我现在明白了。现在我必须弄清楚如何删除一个食谱及其对应的成分(来自 4 个不同的表!),这会很有趣!
    【解决方案2】:

    这是今天早上让它工作的代码。

    我遇到的 NOT NULL 问题是由于路由与删除配方路由的地址相同,所以它在做同样的事情。我认为这是一个模态问题,所以花了一些时间研究 Bootstrap 模态,结果发现我在前端一切正常。

    添加成分行后,我开始获得一些适当的调试信息,并且从那里开始相对简单。

    @app.route('/recipe/ingredient/<int:ingredient_id>/delete', methods=['POST'])
    @login_required
    def deleteingredient(ingredient_id):
        ingredient = Ingredient.query.get_or_404(ingredient_id)
        db.session.delete(ingredient)
        db.session.commit()
        flash('Your ingredient has been deleted', 'success')
        return redirect(url_for('recipe', recipe_id=ingredient.recipe_id))
    

    【讨论】:

      猜你喜欢
      • 2011-02-14
      • 1970-01-01
      • 2014-07-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-13
      • 1970-01-01
      相关资源
      最近更新 更多