fix: resolve circular FK between users and accounts on registration

Account.owner_id and User.account_id are both NOT NULL, creating a
circular dependency that prevents inserting either row first. Fix by:
1. Making owner_id nullable (set immediately after user creation)
2. Creating Account before User, then setting owner_id after flush
3. Removing NOT NULL enforcement on owner_id in migration 020

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
chihlasm
2026-02-07 02:55:53 -05:00
parent 7a6f839ef4
commit 974e86a502
3 changed files with 29 additions and 21 deletions

View File

@@ -81,8 +81,7 @@ def upgrade() -> None:
['account_id'], ['id'], ondelete='CASCADE'
)
# 4. Accounts: enforce owner_id NOT NULL + FK
op.alter_column('accounts', 'owner_id', nullable=False)
# 4. Accounts: add owner FK (owner_id stays nullable due to circular FK with users)
op.create_foreign_key(
'fk_accounts_owner_id', 'accounts', 'users',
['owner_id'], ['id'], ondelete='RESTRICT'