sqlalchemy删除功能问题,我想删除一个项目,但实际上删除了整个类别?

时间:2018-09-09 19:32:33

标签: python sql sqlite flask sqlalchemy

this is my github link

我正在为此项目使用sqlalchemy。

CRUD的删除功能运行正常。

只是想知道会发生什么?

# Edit the specific item
@app.route('/catalog/<int:categories_id>/<int:items_id>/edit',
           methods=['GET', 'POST'])
@login_required
def editItem(categories_id, items_id):
    editedItem = session.query(CategoryItem).filter_by(id=items_id).one()
...
    if request.method == 'POST':
...
        session.add(editedItem)
        session.commit()
        flash("item edited successfully!")
        return redirect(url_for('showItem', categories_id=categories_id,
                                items_id=items_id))
    else:
        return render_template('edititem.html', categories_id=categories_id,
                               items_id=items_id, item=editedItem)


# Delete the specific item
@app.route('/catalog/<int:categories_id>/<int:items_id>/delete',
           methods=['GET', 'POST'])
@login_required
def deleteItem(categories_id, items_id):
    itemToDelete = session.query(CategoryItem).filter_by(id=items_id).one()
...
    if request.method == 'POST':
        session.delete(itemToDelete)
        session.commit()
        flash("item deleted successfully!")
        return redirect(url_for('showCategories', categories_id=categories_id))
    else:
        return render_template('deleteitem.html', categories_id=categories_id,
                               items_id=items_id, item=itemToDelete)

如代码中所示。

editedItem和deleteItem 都使用 session.query(CategoryItem).filter_by(id = items_id).one()

但是当我删除项目时。

而不是删除单个项目,而是删除整个类别。

请参见下面的模板

<form action="{{url_for('deleteItem' ,categories_id=categories_id, items_id=items_id, item=itemToDelete)}}" method="POST">&nbsp
  <input type="submit" value ="Delete">
  <a href = "{{url_for('showItem',categories_id=categories_id, items_id=items_id)}}"><button type="button">Cancel</button></a>

我不确定是什么引起了这个问题。

1 个答案:

答案 0 :(得分:1)

这是因为您设置了与级联delete-orphan的关系。

class Categories(Base):
    __tablename__ = 'categories'
    id = Column(Integer, primary_key=True)
    name = Column(String(250), nullable=False)
    user_id = Column(Integer, ForeignKey('user.id'))
    user = relationship(User)

    @property
    def serialize(self):
        return {
            'name': self.name,
            'id': self.id,
        }


class CategoryItem(Base):
    __tablename__ = 'category_item'
    name = Column(String(80), nullable=False)
    id = Column(Integer, primary_key=True)
    description = Column(String(250))
    categories_id = Column(Integer, ForeignKey('categories.id'))
    categories = relationship(Categories, cascade="all, delete-orphan",
                              single_parent=True)
    user_id = Column(Integer, ForeignKey('user.id'))
    user = relationship(User)

    @property
    def serialize(self):
        return {
            'name': self.name,
            'description': self.description,
            'id': self.id,
        }

您的CategoryItem表是父表。您正在其中将Categories作为子对象设置一个关系。当您拥有cascage='delete-orphan'时,这包括级联删除,但具有增加的行为(您可以在此处了解更多信息:http://docs.sqlalchemy.org/en/latest/orm/cascades.html#delete

尝试从级联参数中删除delete-orphan