Python 为什么只删除产品,而不删除其相关运动?

Python 为什么只删除产品,而不删除其相关运动?,python,flask,sqlalchemy,flask-sqlalchemy,Python,Flask,Sqlalchemy,Flask Sqlalchemy,我试图从product表中删除一个产品,同时也从product movements表中删除该产品的所有移动(称为移动),但只删除该产品,而不删除其移动。有人知道为什么吗?代码如下: @app.route('/products//delete',methods=['GET','POST']) def delete_产品(产品id): product=product.query.get\u或\u 404(产品\u id) movements=Movement.query.filter\u by(pr

我试图从product表中删除一个产品,同时也从product movements表中删除该产品的所有移动(称为移动),但只删除该产品,而不删除其移动。有人知道为什么吗?代码如下:

@app.route('/products//delete',methods=['GET','POST'])
def delete_产品(产品id):
product=product.query.get\u或\u 404(产品\u id)
movements=Movement.query.filter\u by(product\u id=product\u id).all()
对于移动中的移动:
db.session.delete(移动)
db.session.delete(产品)
db.session.commit()
flash(“产品已被删除!”,“成功”)
返回重定向(url_用于('products'))
这是产品表的模型:

类产品(db.Model):
id=db.Column(db.Integer,主键=True)
name=db.Column(db.String(50),unique=True,nullable=False)
产品移动=数据库关系('Movement',backref='item',lazy=True)
这是移动台的一个:

类移动(db.Model):
id=db.Column(db.Integer,主键=True)
product_id=db.Column(db.Integer,db.ForeignKey('product.id'))
product=db.Column(db.String(50),null=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)

我发现了问题所在,我没有在移动表中设置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)

它现在可以工作了。

我发现了问题所在,我没有在移动表中设置产品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)

现在可以了。

这对我使用sqlite和sqlalchemy很有效。可能
移动。产品id
设置不正确?您可以尝试在
Movement.product\u id
上设置
nullable=False
,这将阻止创建没有产品的运动,或者删除没有删除其运动的产品。这对我使用sqlite和sqlalchemy很有效。可能
移动。产品id
设置不正确?您可以尝试在
Movement.product\u id
上设置
nullable=False
,这将阻止创建没有产品的移动,或者在不删除移动的情况下删除产品。