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..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 @@ -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,17 @@ def downgrade(): } ) - op.drop_constraint(None, 'users', type_='foreignkey') + # Find and drop the actual foreign key constraint + 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') op.drop_column('users', 'library_id') # ### end Alembic commands ### \ No newline at end of file