"""Add user_id FK to locations table. Revision ID: 032 Revises: 031 Create Date: 2026-02-26 """ from alembic import op import sqlalchemy as sa revision = "032" down_revision = "031" branch_labels = None depends_on = None def upgrade() -> None: op.add_column("locations", sa.Column("user_id", sa.Integer(), nullable=True)) op.execute( "UPDATE locations SET user_id = (" " SELECT id FROM users WHERE role = 'admin' ORDER BY id LIMIT 1" ")" ) op.create_foreign_key( "fk_locations_user_id", "locations", "users", ["user_id"], ["id"], ondelete="CASCADE" ) op.alter_column("locations", "user_id", nullable=False) op.create_index("ix_locations_user_id", "locations", ["user_id"]) def downgrade() -> None: op.drop_index("ix_locations_user_id", table_name="locations") op.drop_constraint("fk_locations_user_id", "locations", type_="foreignkey") op.drop_column("locations", "user_id")