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"