feat: support cascade delete entity

This commit is contained in:
arkohut 2024-08-29 13:24:23 +08:00
parent 16ef927c38
commit 439adfa955

View File

@ -73,10 +73,16 @@ class EntityModel(Base):
"FolderModel", back_populates="entities" "FolderModel", back_populates="entities"
) )
metadata_entries: Mapped[List["EntityMetadataModel"]] = relationship( metadata_entries: Mapped[List["EntityMetadataModel"]] = relationship(
"EntityMetadataModel", lazy="joined" "EntityMetadataModel",
lazy="joined",
cascade="all, delete-orphan"
) )
tags: Mapped[List["TagModel"]] = relationship( tags: Mapped[List["TagModel"]] = relationship(
"TagModel", secondary="entity_tags", lazy="joined" "TagModel",
secondary="entity_tags",
lazy="joined",
cascade="all, delete",
overlaps="entities"
) )
# 添加索引 # 添加索引
@ -100,7 +106,7 @@ class TagModel(Base):
class EntityTagModel(Base): class EntityTagModel(Base):
__tablename__ = "entity_tags" __tablename__ = "entity_tags"
entity_id: Mapped[int] = mapped_column( entity_id: Mapped[int] = mapped_column(
Integer, ForeignKey("entities.id"), nullable=False Integer, ForeignKey("entities.id", ondelete="CASCADE"), nullable=False
) )
tag_id: Mapped[int] = mapped_column(Integer, ForeignKey("tags.id"), nullable=False) tag_id: Mapped[int] = mapped_column(Integer, ForeignKey("tags.id"), nullable=False)
source: Mapped[MetadataSource] = mapped_column(Enum(MetadataSource), nullable=False) source: Mapped[MetadataSource] = mapped_column(Enum(MetadataSource), nullable=False)