diff --git a/backend/app/api/routes/printers.py b/backend/app/api/routes/printers.py index ca8bae616..3f924859f 100644 --- a/backend/app/api/routes/printers.py +++ b/backend/app/api/routes/printers.py @@ -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 diff --git a/backend/app/models/printer.py b/backend/app/models/printer.py index 0d56b206e..7f736f425 100644 --- a/backend/app/models/printer.py +++ b/backend/app/models/printer.py @@ -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" diff --git a/backend/app/models/smart_plug.py b/backend/app/models/smart_plug.py index 1d9160ef9..6a1238bd2 100644 --- a/backend/app/models/smart_plug.py +++ b/backend/app/models/smart_plug.py @@ -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 diff --git a/test_backend.sh b/test_backend.sh index 56aadf62a..38e0c415b 100755 --- a/test_backend.sh +++ b/test_backend.sh @@ -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 ..