亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

為什么只刪除產品,而不刪除其相關動作?

為什么只刪除產品,而不刪除其相關動作?

皈依舞 2023-08-22 16:30:37
我試圖從產品表中刪除產品,同時也從產品移動表中刪除該產品的所有移動(稱為移動),但僅刪除產品,而不刪除其移動。有誰知道為什么?這是代碼:@app.route('/products/<int:product_id>/delete', methods=['GET', 'POST'])   def delete_product(product_id):    product = Product.query.get_or_404(product_id)    movements = Movement.query.filter_by(product_id=product_id).all()    for movement in movements:        db.session.delete(movement)    db.session.delete(product)    db.session.commit()    flash('The product has been deleted!', 'success')    return redirect(url_for('products'))這是產品表的模型:class Product(db.Model):    id = db.Column(db.Integer, primary_key=True)    name = db.Column(db.String(50), unique=True, nullable=False)    product_movements = db.relationship('Movement', backref='item', lazy=True)這是運動表:class Movement(db.Model):    id = db.Column(db.Integer, primary_key=True)    product_id = db.Column(db.Integer, db.ForeignKey('product.id'))    product = db.Column(db.String(50), nullable=False)    from_location_id = db.Column(db.Integer, db.ForeignKey('location.id'))    from_location = db.Column(db.String(50))    to_location_id = db.Column(db.Integer, db.ForeignKey('location.id'))    to_location = db.Column(db.String(50))    quantity = db.Column(db.Integer, nullable=False)    timestamp = db.Column(db.DateTime, nullable=False, default=datetime.utcnow)
查看完整描述

1 回答

?
慕標琳琳

TA貢獻1830條經驗 獲得超9個贊

我發現出了什么問題,我沒有在移動表中設置product_id 的值。


    if form.validate_on_submit():

        product = Product.query.filter_by(id=form.product.data).first()

        from_location = Location.query.filter_by(id=form.from_location.data).first()

        to_location = Location.query.filter_by(id=form.to_location.data).first()

        m = Movement(product_id = product.id, product = product.name, from_location_id = from_location.id, from_location = from_location.name, to_location_id = to_location.id, to_location = to_location.name, quantity = form.quantity.data)

        db.session.add(m)

        db.session.commit()

        movements = Movement.query.all()

        products = Product.query.all()

        locations = Location.query.all()

        return render_template('movements.html', movements=movements, products=products, locations=locations, form=form)

現在可以了。


查看完整回答
反對 回復 2023-08-22
  • 1 回答
  • 0 關注
  • 1451 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號