Files
aiagent/backend/alembic/versions/012_schedule_fk_set_null.py
renjianbo f30997c02a fix: schedule_delete 外键约束导致无法删除定时任务
- schedule_delete_tool 执行前先解除 executions.schedule_id 引用
- 迁移 012: executions.schedule_id FK 改为 ON DELETE SET NULL

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-05-07 23:08:31 +08:00

44 lines
1.2 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""alter executions.schedule_id FK to ON DELETE SET NULL
Revision ID: 012_schedule_fk_set_null
Revises: 011_add_agent_market_fields
Create Date: 2026-05-07
"""
from alembic import op
revision = "012_schedule_fk_set_null"
down_revision = "011_add_agent_market_fields"
branch_labels = None
depends_on = None
def upgrade() -> None:
"""将 executions.schedule_id 外键改为 SET NULL删除定时任务时自动保留执行记录。"""
try:
op.drop_constraint("executions_ibfk_3", "executions", type_="foreignkey")
except Exception:
# FK name may vary; try alternate patterns
try:
op.drop_constraint("executions_ibfk_4", "executions", type_="foreignkey")
except Exception:
pass
op.create_foreign_key(
"executions_ibfk_3",
"executions", "agent_schedules",
["schedule_id"], ["id"],
ondelete="SET NULL",
)
def downgrade() -> None:
try:
op.drop_constraint("executions_ibfk_3", "executions", type_="foreignkey")
except Exception:
pass
op.create_foreign_key(
"executions_ibfk_3",
"executions", "agent_schedules",
["schedule_id"], ["id"],
ondelete="RESTRICT",
)