From 9c845eeb9e107e4cd44ae0257803a3b9d1ef49a0 Mon Sep 17 00:00:00 2001 From: Talmaj Marinc Date: Tue, 30 Jun 2026 09:53:09 +0200 Subject: [PATCH] Fix an issue when a credential is deleted, resuming of download fails. --- app/model_downloader/database/models.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/app/model_downloader/database/models.py b/app/model_downloader/database/models.py index a7a8fde88..2b0c9fffe 100644 --- a/app/model_downloader/database/models.py +++ b/app/model_downloader/database/models.py @@ -65,7 +65,12 @@ class Download(Base): expected_sha256: Mapped[str | None] = mapped_column(String(64), nullable=True) # Explicit credential override; otherwise auto-resolved by host. - credential_id: Mapped[str | None] = mapped_column(String(36), nullable=True) + # RESTRICT keeps a credential from being deleted while a download references it. + credential_id: Mapped[str | None] = mapped_column( + String(36), + ForeignKey("host_credentials.id", ondelete="RESTRICT"), + nullable=True, + ) allow_any_extension: Mapped[bool] = mapped_column( Boolean, nullable=False, default=False ) @@ -86,6 +91,10 @@ class Download(Base): order_by="DownloadSegment.idx", ) + credential: Mapped[HostCredential | None] = relationship( + "HostCredential", back_populates="downloads" + ) + __table_args__ = ( Index("ix_downloads_status", "status"), Index("ix_downloads_priority", "priority"), @@ -152,6 +161,10 @@ class HostCredential(Base): BigInteger, nullable=False, default=_now, onupdate=_now ) + downloads: Mapped[list[Download]] = relationship( + "Download", back_populates="credential" + ) + __table_args__ = ( Index("uq_host_credentials_host", "host", unique=True), )