Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/297.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 从父级中删除子级而不删除父级时自动删除该子级_Python_Sqlalchemy_Rdbms - Fatal编程技术网

Python 从父级中删除子级而不删除父级时自动删除该子级

Python 从父级中删除子级而不删除父级时自动删除该子级,python,sqlalchemy,rdbms,Python,Sqlalchemy,Rdbms,有一对多的关系。在sqlalchemy中,它用此代码表示 class Parent(Base): __tablename__ = 'parent' id = Column(Integer, primary_key=True) children = relationship("Child") class Child(Base): __tablename__ = 'child' id = Column(Integer, primary_k

有一对多的关系。在
sqlalchemy
中,它用此代码表示

class Parent(Base):
    __tablename__ = 'parent'
    id = Column(Integer, primary_key=True)
    children = relationship("Child")

class Child(Base):
    __tablename__ = 'child'
    id = Column(Integer, primary_key=True)
    parent_id = Column(Integer, ForeignKey('parent.id'))
在我看来,没有父母,孩子就不可能存在。因此
parent\u id
不允许为
NULL
。当我从父级
中删除
子级
时,它应该被自动删除。我不确定这是否可行,或者这是否是关系数据库管理系统概念的一部分

目前我有

child = parent.children[0]
parent.children.remove(child)
session.delete(child)
我想要的是这个

child = parent.children[0]
parent.children.remove(child)
还是这个

child = parent.children[0]
session.delete(child)