第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

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

為什么只刪除產(chǎn)品,而不刪除其相關(guān)動作?

為什么只刪除產(chǎn)品,而不刪除其相關(guān)動作?

皈依舞 2023-08-22 16:30:37
我試圖從產(chǎn)品表中刪除產(chǎn)品,同時也從產(chǎn)品移動表中刪除該產(chǎn)品的所有移動(稱為移動),但僅刪除產(chǎn)品,而不刪除其移動。有誰知道為什么?這是代碼:@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'))這是產(chǎn)品表的模型: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 回答

?
慕標(biāo)琳琳

TA貢獻(xiàn)1830條經(jīng)驗 獲得超9個贊

我發(fā)現(xiàn)出了什么問題,我沒有在移動表中設(shè)置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)

現(xiàn)在可以了。


查看完整回答
反對 回復(fù) 2023-08-22
  • 1 回答
  • 0 關(guān)注
  • 1431 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號