Fix printer deletion freeze and allow multiple smart plugs per printer (Issue #214)

Bug 1: Delete printer was not actually deleting archives when
delete_archives=True (default). The if-condition was inverted, causing
archives to remain and potentially causing FK constraint issues.

Bug 2: SmartPlug.printer_id had unique=True constraint, preventing
multiple HA scripts from being linked to the same printer. Removed the
constraint to allow multiple plugs/scripts per printer (matching
feature/176 behavior).

Closes #214
This commit is contained in:
maziggy
2026-02-01 14:39:04 +01:00
parent 0b3df17920
commit 2d445badac
4 changed files with 9 additions and 8 deletions
+4 -1
View File
@@ -175,7 +175,10 @@ async def delete_printer(
printer_manager.disconnect_printer(printer_id)
if not delete_archives:
if delete_archives:
# Delete all archives for this printer
await db.execute(sql_delete(PrintArchive).where(PrintArchive.printer_id == printer_id))
else:
# Orphan the archives instead of deleting them
from sqlalchemy import update
+1 -1
View File
@@ -40,7 +40,7 @@ class Printer(Base):
# Relationships
archives: Mapped[list["PrintArchive"]] = relationship(back_populates="printer", cascade="all, delete-orphan")
smart_plug: Mapped["SmartPlug | None"] = relationship(back_populates="printer", uselist=False)
smart_plugs: Mapped[list["SmartPlug"]] = relationship(back_populates="printer")
notification_providers: Mapped[list["NotificationProvider"]] = relationship(back_populates="printer")
maintenance_items: Mapped[list["PrinterMaintenance"]] = relationship(
back_populates="printer", cascade="all, delete-orphan"
+3 -5
View File
@@ -50,10 +50,8 @@ class SmartPlug(Base):
# Legacy multiplier - kept for backward compatibility
mqtt_multiplier: Mapped[float] = mapped_column(Float, default=1.0) # Deprecated, use mqtt_power_multiplier
# Link to printer (1:1)
printer_id: Mapped[int | None] = mapped_column(
ForeignKey("printers.id", ondelete="SET NULL"), unique=True, nullable=True
)
# Link to printer (multiple plugs/scripts can be linked to one printer)
printer_id: Mapped[int | None] = mapped_column(ForeignKey("printers.id", ondelete="SET NULL"), nullable=True)
# Automation settings
enabled: Mapped[bool] = mapped_column(Boolean, default=True)
@@ -100,7 +98,7 @@ class SmartPlug(Base):
updated_at: Mapped[datetime] = mapped_column(DateTime, server_default=func.now(), onupdate=func.now())
# Relationship
printer: Mapped["Printer"] = relationship(back_populates="smart_plug")
printer: Mapped["Printer"] = relationship(back_populates="smart_plugs")
from backend.app.models.printer import Printer # noqa: E402
+1 -1
View File
@@ -1,5 +1,5 @@
#!/bin/sh
cd backend
../venv/bin/python3 -m pytest tests/ -v
../venv/bin/python3 -m pytest tests/ -v -n 14
cd ..