Skip to content

Commit

Permalink
Update foreign key migration
Browse files Browse the repository at this point in the history
Update foreign key migration
  • Loading branch information
pablonyx authored Jan 31, 2025
2 parents 17b280e + ffe8ac1 commit 3e0d24a
Showing 1 changed file with 14 additions and 11 deletions.
25 changes: 14 additions & 11 deletions backend/alembic/versions/33ea50e88f24_foreign_key_input_prompts.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,18 @@


def upgrade() -> None:
# First drop the existing FK constraints
op.drop_constraint(
"inputprompt__user_input_prompt_id_fkey",
"inputprompt__user",
type_="foreignkey",
# Safely drop constraints if exists
op.execute(
"""
ALTER TABLE inputprompt__user
DROP CONSTRAINT IF EXISTS inputprompt__user_input_prompt_id_fkey
"""
)
op.drop_constraint(
"inputprompt__user_user_id_fkey",
"inputprompt__user",
type_="foreignkey",
op.execute(
"""
ALTER TABLE inputprompt__user
DROP CONSTRAINT IF EXISTS inputprompt__user_user_id_fkey
"""
)

# Recreate with ON DELETE CASCADE
Expand All @@ -37,10 +39,11 @@ def upgrade() -> None:
["id"],
ondelete="CASCADE",
)

op.create_foreign_key(
"inputprompt__user_user_id_fkey",
"inputprompt__user",
'"user"',
"user",
["user_id"],
["id"],
ondelete="CASCADE",
Expand Down Expand Up @@ -71,7 +74,7 @@ def downgrade() -> None:
op.create_foreign_key(
"inputprompt__user_user_id_fkey",
"inputprompt__user",
'"user"',
"user",
["user_id"],
["id"],
)

0 comments on commit 3e0d24a

Please sign in to comment.