Files
KCGL/inventory-backend/migrations/versions/a1b2c3d4e5f6_add_purchase_stockbuy_link.py

80 lines
2.8 KiB
Python
Raw Normal View History

"""打通采购申请与采购入库的按单入库链路
Revision ID: a1b2c3d4e5f6
Revises: None (或填当前最新 revision)
Create Date: 2026-07-14
变更内容:
1. purchase_request 表新增 base_id (FK → material_base.id)
2. stock_buy 表新增 request_id (FK → purchase_request.id)
"""
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision = 'a1b2c3d4e5f6'
down_revision = None # ← 替换为当前最新 revision ID
branch_labels = None
depends_on = None
def upgrade():
# ============================================================
# 1. purchase_request 新增 base_id
# ============================================================
with op.batch_alter_table('purchase_request', schema=None) as batch_op:
batch_op.add_column(
sa.Column('base_id', sa.Integer(), nullable=True,
comment='关联基础物料ID')
)
batch_op.create_foreign_key(
'fk_purchase_request_base_id',
'material_base',
['base_id'],
['id'],
ondelete='SET NULL'
)
batch_op.create_index(
'ix_purchase_request_base_id',
['base_id']
)
# ============================================================
# 2. stock_buy 新增 request_id
# ============================================================
with op.batch_alter_table('stock_buy', schema=None) as batch_op:
batch_op.add_column(
sa.Column('request_id', sa.Integer(), nullable=True,
comment='关联采购申请单ID')
)
batch_op.create_foreign_key(
'fk_stock_buy_request_id',
'purchase_request',
['request_id'],
['id'],
ondelete='SET NULL'
)
batch_op.create_index(
'ix_stock_buy_request_id',
['request_id']
)
def downgrade():
# ============================================================
# 回滚:stock_buy 删除 request_id
# ============================================================
with op.batch_alter_table('stock_buy', schema=None) as batch_op:
batch_op.drop_constraint('fk_stock_buy_request_id', type_='foreignkey')
batch_op.drop_index('ix_stock_buy_request_id')
batch_op.drop_column('request_id')
# ============================================================
# 回滚:purchase_request 删除 base_id
# ============================================================
with op.batch_alter_table('purchase_request', schema=None) as batch_op:
batch_op.drop_constraint('fk_purchase_request_base_id', type_='foreignkey')
batch_op.drop_index('ix_purchase_request_base_id')
batch_op.drop_column('base_id')