From 72ac1d53fb0fb27533cd6323df4777e7a0d70cf5 Mon Sep 17 00:00:00 2001 From: Kyle Pope Date: Fri, 27 Feb 2026 04:49:57 +0800 Subject: [PATCH] Fix migration 030 failing on fresh DB with no admin user MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Migration 006 seeds default calendar rows. On a fresh install, no users exist when migration 030 runs, so the backfill SELECT returns NULL and SET NOT NULL fails. Now deletes orphan calendars before enforcing the constraint — account setup will recreate defaults for new users. Co-Authored-By: Claude Opus 4.6 --- backend/alembic/versions/030_add_user_id_to_calendars.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/backend/alembic/versions/030_add_user_id_to_calendars.py b/backend/alembic/versions/030_add_user_id_to_calendars.py index ef1b621..23aafe1 100644 --- a/backend/alembic/versions/030_add_user_id_to_calendars.py +++ b/backend/alembic/versions/030_add_user_id_to_calendars.py @@ -15,11 +15,15 @@ depends_on = None def upgrade() -> None: op.add_column("calendars", sa.Column("user_id", sa.Integer(), nullable=True)) + # Backfill existing calendars to first admin user op.execute( "UPDATE calendars SET user_id = (" " SELECT id FROM users WHERE role = 'admin' ORDER BY id LIMIT 1" ")" ) + # On fresh installs no users exist yet, so seeded calendars still have + # NULL user_id. Remove them — account setup will recreate defaults. + op.execute("DELETE FROM calendars WHERE user_id IS NULL") op.create_foreign_key( "fk_calendars_user_id", "calendars", "users", ["user_id"], ["id"], ondelete="CASCADE"