[GH-ISSUE #689] [Bug]: can't link more than one smart switch to a printer #455

Closed
opened 2026-05-06 12:29:44 +02:00 by BreizhHardware · 10 comments

Originally created by @ManuelW77 on GitHub (Mar 13, 2026).
Original GitHub issue: https://github.com/maziggy/bambuddy/issues/689

Originally assigned to: @maziggy on GitHub.

Bug Description

Found a bug at settings smart switches.
I want to link a second smart switch to a printer, for a heater.
But if i try to link the second switch i get a error 500.

I found that the database has a constraint that only one printer_id can be set.

[bambuddy] 2026-03-13T09:22:17.856100165Z sqlalchemy.exc.IntegrityError: (sqlite3.IntegrityError) UNIQUE constraint failed: smart_plugs.printer_id
[bambuddy] 2026-03-13T09:22:17.856132153Z [SQL: UPDATE smart_plugs SET printer_id=?, updated_at=CURRENT_TIMESTAMP WHERE smart_plugs.id = ?]

Expected Behavior

error 500

Steps to Reproduce

create a second switch an try to link to a printer which has already a switch.
get error 500

Printer Model

P1S

Bambuddy Version

v0.2.2b3

Printer Firmware Version

01.08.0000

Installation Method

Docker

Operating System

Linux (Ubuntu/Debian)

Relevant Logs / Support Package


Screenshots

No response

Additional Context

No response

Checklist

  • I have searched existing issues to ensure this bug hasn't already been reported
  • I am using the latest version of Bambuddy
  • My printer is set to LAN Only mode
  • My printer has Developer Mode enabled
Originally created by @ManuelW77 on GitHub (Mar 13, 2026). Original GitHub issue: https://github.com/maziggy/bambuddy/issues/689 Originally assigned to: @maziggy on GitHub. ### Bug Description Found a bug at settings smart switches. I want to link a second smart switch to a printer, for a heater. But if i try to link the second switch i get a error 500. I found that the database has a constraint that only one printer_id can be set. [bambuddy] 2026-03-13T09:22:17.856100165Z sqlalchemy.exc.IntegrityError: (sqlite3.IntegrityError) UNIQUE constraint failed: smart_plugs.printer_id [bambuddy] 2026-03-13T09:22:17.856132153Z [SQL: UPDATE smart_plugs SET printer_id=?, updated_at=CURRENT_TIMESTAMP WHERE smart_plugs.id = ?] ### Expected Behavior error 500 ### Steps to Reproduce create a second switch an try to link to a printer which has already a switch. get error 500 ### Printer Model P1S ### Bambuddy Version v0.2.2b3 ### Printer Firmware Version 01.08.0000 ### Installation Method Docker ### Operating System Linux (Ubuntu/Debian) ### Relevant Logs / Support Package ```shell ``` ### Screenshots _No response_ ### Additional Context _No response_ ### Checklist - [x] I have searched existing issues to ensure this bug hasn't already been reported - [x] I am using the latest version of Bambuddy - [x] My printer is set to LAN Only mode - [x] My printer has Developer Mode enabled
BreizhHardware 2026-05-06 12:29:44 +02:00
  • closed this issue
  • added the
    bug
    label
Author
Owner

@maziggy commented on GitHub (Mar 13, 2026):

Cannot reproduce the issue. Please upload a support package.

Image Image
<!-- gh-comment-id:4054607516 --> @maziggy commented on GitHub (Mar 13, 2026): Cannot reproduce the issue. Please upload a support package. <img width="441" height="928" alt="Image" src="https://github.com/user-attachments/assets/d63eefc1-988b-4990-8570-7048b56ce345" /> <img width="608" height="178" alt="Image" src="https://github.com/user-attachments/assets/24c5a48b-17f4-4a45-a627-0ab283050ff4" />
Author
Owner

@ManuelW77 commented on GitHub (Mar 13, 2026):

Yes, you added a first switch. now add a second switch to the same printer.
https://github.com/maziggy/bambuddy/issues/691#issue-4070739522

<!-- gh-comment-id:4054672821 --> @ManuelW77 commented on GitHub (Mar 13, 2026): Yes, you added a first switch. now add a second switch to the same printer. https://github.com/maziggy/bambuddy/issues/691#issue-4070739522
Author
Owner

@maziggy commented on GitHub (Mar 13, 2026):

My primary switch is a Tasmota switch added to Bambuddy. Secondary switch is the HA switch entity.

<!-- gh-comment-id:4054684103 --> @maziggy commented on GitHub (Mar 13, 2026): My primary switch is a Tasmota switch added to Bambuddy. Secondary switch is the HA switch entity.
Author
Owner

@ManuelW77 commented on GitHub (Mar 13, 2026):

i try to add two ha switches to a single printer.

<!-- gh-comment-id:4054693469 --> @ManuelW77 commented on GitHub (Mar 13, 2026): i try to add two ha switches to a single printer.
Author
Owner

@ManuelW77 commented on GitHub (Mar 13, 2026):

the debug log says it, there are only one printer_id allowed at this table.

[bambuddy] 2026-03-13T09:22:17.856100165Z sqlalchemy.exc.IntegrityError: (sqlite3.IntegrityError) UNIQUE constraint failed: smart_plugs.printer_id
[bambuddy] 2026-03-13T09:22:17.856132153Z [SQL: UPDATE smart_plugs SET printer_id=?, updated_at=CURRENT_TIMESTAMP WHERE smart_plugs.id = ?]```
<!-- gh-comment-id:4054699876 --> @ManuelW77 commented on GitHub (Mar 13, 2026): the debug log says it, there are only one printer_id allowed at this table. ``` [bambuddy] 2026-03-13T09:22:17.856100165Z sqlalchemy.exc.IntegrityError: (sqlite3.IntegrityError) UNIQUE constraint failed: smart_plugs.printer_id [bambuddy] 2026-03-13T09:22:17.856132153Z [SQL: UPDATE smart_plugs SET printer_id=?, updated_at=CURRENT_TIMESTAMP WHERE smart_plugs.id = ?]```
Author
Owner

@maziggy commented on GitHub (Mar 13, 2026):

Ahhhh...got it.

<!-- gh-comment-id:4054725147 --> @maziggy commented on GitHub (Mar 13, 2026): Ahhhh...got it.
Author
Owner

@maziggy commented on GitHub (Mar 13, 2026):

The migration that removes the UNIQUE constraint on printer_id was silently skipping your database because it used an exact string match to detect the constraint, and your database (created with an older version) has the constraint expressed differently in the SQLite schema.

This is fixed in dev branch and available in the next release — the migration now uses pattern matching that handles all the ways SQLite can express a UNIQUE constraint (inline, table-level, quoted column names, standalone indexes).

Workaround until the update: You can fix your database manually by running:

docker exec -it bambuddy sqlite3 /app/data/bambuddy.db

Then paste:

-- Check current schema
SELECT sql FROM sqlite_master WHERE type='table' AND name='smart_plugs';

-- If it shows UNIQUE on printer_id, the migration didn't run.
-- The next update will fix this automatically.

Multiple HA switches per printer is already supported — this was just the migration not detecting the old constraint in your database.

<!-- gh-comment-id:4054808082 --> @maziggy commented on GitHub (Mar 13, 2026): The migration that removes the UNIQUE constraint on printer_id was silently skipping your database because it used an exact string match to detect the constraint, and your database (created with an older version) has the constraint expressed differently in the SQLite schema. This is fixed in dev branch and available in the next release — the migration now uses pattern matching that handles all the ways SQLite can express a UNIQUE constraint (inline, table-level, quoted column names, standalone indexes). Workaround until the update: You can fix your database manually by running: docker exec -it bambuddy sqlite3 /app/data/bambuddy.db Then paste: -- Check current schema SELECT sql FROM sqlite_master WHERE type='table' AND name='smart_plugs'; -- If it shows UNIQUE on printer_id, the migration didn't run. -- The next update will fix this automatically. Multiple HA switches per printer is already supported — this was just the migration not detecting the old constraint in your database.
Author
Owner

@maziggy commented on GitHub (Mar 13, 2026):

Does it work now for you?

<!-- gh-comment-id:4055871945 --> @maziggy commented on GitHub (Mar 13, 2026): Does it work now for you?
Author
Owner

@ManuelW77 commented on GitHub (Mar 13, 2026):

I'm waiting for the next b4 package :)

<!-- gh-comment-id:4056077948 --> @ManuelW77 commented on GitHub (Mar 13, 2026): I'm waiting for the next b4 package :)
Author
Owner

@ManuelW77 commented on GitHub (Mar 13, 2026):

I've tested the daily beta and now it works :)

<!-- gh-comment-id:4057940427 --> @ManuelW77 commented on GitHub (Mar 13, 2026): I've tested the daily beta and now it works :)
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/bambuddy#455
No description provided.