Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/361.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python SQLAlchemy:不使用session.Delete()直接从一对多关系中删除对象_Python_Mysql_Session_Sqlalchemy_One To Many - Fatal编程技术网

Python SQLAlchemy:不使用session.Delete()直接从一对多关系中删除对象

Python SQLAlchemy:不使用session.Delete()直接从一对多关系中删除对象,python,mysql,session,sqlalchemy,one-to-many,Python,Mysql,Session,Sqlalchemy,One To Many,我有以下SQLAlchemy设置: Base = declarative_base() class Post(Base): __tablename__ = 'post' id = Column(Integer, primary_key=True) title = Column(String(30)) comments = relationship('Comment', cascade='all') class Comment(Base): __tabl

我有以下SQLAlchemy设置:

Base = declarative_base()

class Post(Base):
    __tablename__ = 'post'
    id = Column(Integer, primary_key=True)
    title = Column(String(30))
    comments = relationship('Comment', cascade='all')

class Comment(Base):
    __tablename__ = 'comment'
    id = Column(Integer, primary_key=True)
    post_id = Column(Integer, ForeignKey(Post.id, ondelete='CASCADE'), nullable=False)
    text = Column(Text)
这样,我就可以创建与评论具有一对多关系的post对象。我想在不引用会话的情况下处理帖子评论的创建和删除。在帖子中添加评论效果很好:

post = Post(title='some post')
comment = Comment(text='some comment')
post.comments.append(comment)
我的会话处理程序只知道post,因此它将执行一个
会话。添加(post)
,注释将自动放入会话中,并在下一个
会话中与数据库同步。commit()
。但是,删除注释的情况并非如此。我希望能够通过执行以下操作删除评论:

post.comments.remove(comment)
但是,这会在下一个
会话中产生以下错误。commit()

我如何告诉SQLAlchemy不要使用post_id的
NULL
值更新注释(由于列上的not NULL约束,这是不允许的),而是删除注释?我知道我可以执行
session.delete(comment)
,但由于我不需要将注释显式添加到会话中,因此我看不出为什么必须将其从会话中显式删除

我找到了几种将删除级联到相关对象的解决方案,但由于我从未向会话发出任何显式删除(帖子仍在那里),我认为这不适用

编辑:我对示例进行了调整,以包括从帖子中删除的级联。现在它可以进行
会话。删除(post)
并删除所有评论。但是我只想自动删除我从关系中删除的评论,而不是删除包含所有评论的整个帖子


TL;DR:当我从一对多关系的关系列表中删除条目时,如何告诉SQLAlchemy发出delete语句而不是update语句?

我不是SQLAlchemy用户,但我认为您应该使用ondelete选项

post_id = Column(Integer, ForeignKey(Post.id, ondelete="CASCADE"), nullable=False) post_id=Column(整数,ForeignKey(post.id,ondelete=“CASCADE”),nullable=False) 请参阅Mysql 5.6手册13.6.44,外键约束

SET NULL: Delete or update the row from the parent table, and set the foreign key column or columns in the child table to NULL. Both ON DELETE SET NULL and ON UPDATE SET NULL clauses are supported. SET NULL:删除或更新父表中的行,并将子表中的外键列设置为NULL。 同时支持ON DELETE SET NULL和ON UPDATE SET NULL子句。 级联:删除或更新父表中的行,自动删除或更新子表中匹配的行。 同时支持删除级联和更新级联。在两个表之间,在更新时不要定义多个表 作用于父表或子表中同一列的级联子句。 及 部分:定义外键->更新时和删除时

有关详细信息,请阅读文档部分,但基本上您需要
删除孤立项
以及
关系的
级联
选项:

class Post(Base):
    # ...
    comments = relationship('Comment', cascade="all, delete-orphan")

使用
session.delete(post)
删除帖子时,这可能会起作用,但我不会这么做。因此,没有什么可以逐级传达给评论。我只想删除注释。使用级联中的
delete orphan
选项,从关系列表中删除时的删除效果与预期一样。谢谢 CASCADE: Delete or update the row from the parent table, and automatically delete or update the matching rows in the child table. Both ON DELETE CASCADE and ON UPDATE CASCADE are supported. Between two tables, do not define several ON UPDATE CASCADE clauses that act on the same column in the parent table or in the child table.
class Post(Base):
    # ...
    comments = relationship('Comment', cascade="all, delete-orphan")