我试图从产品表中删除产品,同时也从产品移动表中删除该产品的所有移动(称为移动),但仅删除产品,而不删除其移动。有谁知道为什么?这是代码:
@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)
慕标琳琳
相关分类