sqlalchemy:从相关表中删除行从多到许多关系中删除行
我注意到,在删除一个条目时,未删除辅助表中的相应行。在这里我的模型:
cashflows_tags_table = Table(
"cashflows_tags",
Base.metadata,
Column("cashflow_id", ForeignKey("cashflows.id"),primary_key=True),
Column("tag_id", ForeignKey("tags.id"), primary_key=True),
)
class Tag(Base):
__tablename__ = "tags"
id = Column(Integer, primary_key=True)
tag = Column(String, nullable=False, unique=True)
class Cashflow(Base):
__tablename__ = "cashflows"
id = Column(Integer, primary_key=True)
date = Column(DateTime, nullable=False)
amount = Column(Float, nullable=False)
description = Column(String, nullable=False)
### Many to Many ###
tags = relationship("Tag", secondary=cashflows_tags_table)
#######
我从文档中了解的内容( https://docs.sqlalchemy.org/en/14/orm/basic_relationships.html#deleting-rows-rows-from-the-the-the-the-the-the-many-to-many-t-many-table )删除应自动传播。其他级联/删除/删除 - 孔口会导致另一个“许多”侧的元素删除,这不是我想要的。
我正在使用sqlite,以及删除的以下ORM语法:
with Session(db_engine) as session:
ddd = delete(Cashflow).where(Cashflow.id.in_(id_list))
session.execute(ddd)
session.commit()
最后
我在删除之前手动选择了该条目并清空标签集合(如其他线程中建议)。我仍然会把问题打开,因为我尚不清楚这是预期的行为
I noticed that when deleting one entry, the corresponding rows from the secondary table are not deleted. here my models:
cashflows_tags_table = Table(
"cashflows_tags",
Base.metadata,
Column("cashflow_id", ForeignKey("cashflows.id"),primary_key=True),
Column("tag_id", ForeignKey("tags.id"), primary_key=True),
)
class Tag(Base):
__tablename__ = "tags"
id = Column(Integer, primary_key=True)
tag = Column(String, nullable=False, unique=True)
class Cashflow(Base):
__tablename__ = "cashflows"
id = Column(Integer, primary_key=True)
date = Column(DateTime, nullable=False)
amount = Column(Float, nullable=False)
description = Column(String, nullable=False)
### Many to Many ###
tags = relationship("Tag", secondary=cashflows_tags_table)
#######
What I understand from the documentation (https://docs.sqlalchemy.org/en/14/orm/basic_relationships.html#deleting-rows-from-the-many-to-many-table) is that the deletion should be propagated automatically. Additional cascade/delete/delete-orphan would cause the deletion of the elements on the other "many" side, which is not what I want.
I am using sqlite, and the following ORM syntax for the deletion:
with Session(db_engine) as session:
ddd = delete(Cashflow).where(Cashflow.id.in_(id_list))
session.execute(ddd)
session.commit()
EDIT
in the end I solved it manually selecting the entry and emptying the tag collection before the deletion (as suggested in other threads). I will still leave the question open, since it is not clear to me if this is the expected behaviour
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您不想设置级联反应, session.delete 将同时删除
cashflow
和相关的多对多记录。它将首先发射
delete
语句,然后CashFlow
s。另外,您也可以 >并将“级联”设置为 onDelete行为当使用 sqlachemy.delete 。
If you don't want to set a cascade, session.delete will remove both the
CashFlow
and the associated many-to-many records.It will emit a
DELETE
statement for the many-to-many records first, then theCashFlow
s.Alternatively, you can enable foreign keys in SQLite and set "CASCADE" as the ondelete behaviour for the foreign key in the association table - only rows associated with the CashFlow to be deleted will be deleted when using sqlachemy.delete.