From 8dbb87e4d2aab6b49a575fad6c07409bfd29f41b Mon Sep 17 00:00:00 2001 From: chihlasm Date: Sat, 7 Feb 2026 03:06:35 -0500 Subject: [PATCH] fix: add migration 021 to make accounts.owner_id nullable on existing DBs Railway already ran the old migration 020 which enforced NOT NULL on owner_id. Since alembic won't re-run a corrected 020, this new migration explicitly reverts the constraint for databases that already applied it. Co-Authored-By: Claude Opus 4.6 --- .../versions/021_fix_owner_id_nullable.py | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 backend/alembic/versions/021_fix_owner_id_nullable.py diff --git a/backend/alembic/versions/021_fix_owner_id_nullable.py b/backend/alembic/versions/021_fix_owner_id_nullable.py new file mode 100644 index 00000000..28322bd2 --- /dev/null +++ b/backend/alembic/versions/021_fix_owner_id_nullable.py @@ -0,0 +1,30 @@ +"""fix accounts.owner_id to be nullable (circular FK with users) + +Revision ID: 021 +Revises: 020 +Create Date: 2026-02-07 + +The original migration 020 enforced NOT NULL on owner_id, but this creates +a circular FK problem: Account needs owner_id (User) and User needs +account_id (Account). Making owner_id nullable resolves this — we create +the Account first, then the User, then set owner_id. +""" +from typing import Sequence, Union + +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision: str = '021' +down_revision: Union[str, None] = '020' +branch_labels: Union[str, Sequence[str], None] = None +depends_on: Union[str, Sequence[str], None] = None + + +def upgrade() -> None: + op.alter_column('accounts', 'owner_id', nullable=True) + + +def downgrade() -> None: + op.alter_column('accounts', 'owner_id', nullable=False)