|
5 | 5 | Create Date: 2025-03-11 16:26:52.406726 |
6 | 6 |
|
7 | 7 | """ |
8 | | -from alembic import op |
9 | 8 |
|
| 9 | +from alembic import op |
| 10 | +from sqlalchemy.engine.reflection import Inspector |
10 | 11 |
|
11 | 12 | # revision identifiers, used by Alembic. |
12 | | -revision = '37406cca756c' |
13 | | -down_revision = 'da444de005a6' |
| 13 | +revision = "37406cca756c" |
| 14 | +down_revision = "da444de005a6" |
14 | 15 | branch_labels = None |
15 | 16 | depends_on = None |
16 | 17 |
|
17 | 18 |
|
18 | 19 | def upgrade(): |
19 | | - # ### commands auto generated by Alembic - please adjust! ### |
20 | | - op.drop_constraint('case_cost_case_cost_type_id_fkey', 'case_cost', type_='foreignkey') |
21 | | - op.create_foreign_key(None, 'case_cost', 'case_cost_type', ['case_cost_type_id'], ['id'], ondelete='CASCADE') |
| 20 | + # Get the connection and inspector |
| 21 | + conn = op.get_bind() |
| 22 | + inspector = Inspector.from_engine(conn) |
| 23 | + |
| 24 | + # Check if the constraint exists |
| 25 | + constraints = inspector.get_foreign_keys("case_cost") |
| 26 | + constraint_names = [constraint["name"] for constraint in constraints] |
| 27 | + |
| 28 | + # Drop the constraint if it exists |
| 29 | + if "case_cost_case_cost_type_id_fkey" in constraint_names: |
| 30 | + op.drop_constraint("case_cost_case_cost_type_id_fkey", "case_cost", type_="foreignkey") |
| 31 | + |
| 32 | + # Create the new foreign key constraint |
| 33 | + op.create_foreign_key( |
| 34 | + None, "case_cost", "case_cost_type", ["case_cost_type_id"], ["id"], ondelete="CASCADE" |
| 35 | + ) |
22 | 36 | # ### end Alembic commands ### |
23 | 37 |
|
24 | 38 |
|
25 | 39 | def downgrade(): |
26 | 40 | # ### commands auto generated by Alembic - please adjust! ### |
27 | | - op.drop_constraint(None, 'case_cost', type_='foreignkey') |
28 | | - op.create_foreign_key('case_cost_case_cost_type_id_fkey', 'case_cost', 'case_cost_type', ['case_cost_type_id'], ['id']) |
| 41 | + op.drop_constraint(None, "case_cost", type_="foreignkey") |
| 42 | + op.create_foreign_key( |
| 43 | + "case_cost_case_cost_type_id_fkey", |
| 44 | + "case_cost", |
| 45 | + "case_cost_type", |
| 46 | + ["case_cost_type_id"], |
| 47 | + ["id"], |
| 48 | + ) |
29 | 49 | # ### end Alembic commands ### |
0 commit comments