From 9afc72527c69184e258a3a36ac11574a12d7beea Mon Sep 17 00:00:00 2001 From: femalves Date: Mon, 11 Aug 2025 15:32:58 -0400 Subject: [PATCH 1/2] fixing falsy values --- ...19_adding_library_id_foreign_key_to_user.py | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/alembic/versions/af63c0205b19_adding_library_id_foreign_key_to_user.py b/alembic/versions/af63c0205b19_adding_library_id_foreign_key_to_user.py index 755fac8..d518507 100644 --- a/alembic/versions/af63c0205b19_adding_library_id_foreign_key_to_user.py +++ b/alembic/versions/af63c0205b19_adding_library_id_foreign_key_to_user.py @@ -24,7 +24,7 @@ def upgrade(): # ### commands auto generated by Alembic - please adjust! ### # Add foreign key constraint to users table op.add_column('users', sa.Column('library_id', sa.Integer(), nullable=True)) - op.create_foreign_key(None, 'users', 'library', ['library_id'], ['id']) + op.create_foreign_key('fk_users_library_id', 'users', 'library', ['library_id'], ['id']) bind = op.get_bind() # Get all users with link_server in user_data @@ -78,6 +78,20 @@ def downgrade(): } ) - op.drop_constraint(None, 'users', type_='foreignkey') + # Find and drop the foreign key constraint dynamically + bind = op.get_bind() + result = bind.execute(""" + SELECT constraint_name + FROM information_schema.table_constraints + WHERE table_name = 'users' + AND constraint_type = 'FOREIGN KEY' + """).fetchone() + + if result: + constraint_name = result[0] + op.drop_constraint(constraint_name, 'users', type_='foreignkey') + else: + # If no constraint found, just drop the column (PostgreSQL will handle it) + pass op.drop_column('users', 'library_id') # ### end Alembic commands ### \ No newline at end of file From 7972675685e2933179e82378692abbb2d3c6d4e8 Mon Sep 17 00:00:00 2001 From: femalves Date: Mon, 11 Aug 2025 16:32:22 -0400 Subject: [PATCH 2/2] fixing falsy values --- .../af63c0205b19_adding_library_id_foreign_key_to_user.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/alembic/versions/af63c0205b19_adding_library_id_foreign_key_to_user.py b/alembic/versions/af63c0205b19_adding_library_id_foreign_key_to_user.py index d518507..64de69b 100644 --- a/alembic/versions/af63c0205b19_adding_library_id_foreign_key_to_user.py +++ b/alembic/versions/af63c0205b19_adding_library_id_foreign_key_to_user.py @@ -78,7 +78,7 @@ def downgrade(): } ) - # Find and drop the foreign key constraint dynamically + # Find and drop the actual foreign key constraint bind = op.get_bind() result = bind.execute(""" SELECT constraint_name @@ -90,8 +90,5 @@ def downgrade(): if result: constraint_name = result[0] op.drop_constraint(constraint_name, 'users', type_='foreignkey') - else: - # If no constraint found, just drop the column (PostgreSQL will handle it) - pass op.drop_column('users', 'library_id') # ### end Alembic commands ### \ No newline at end of file