feat(db): wms_store_warehouse 新增 initiator_subject_id,wms_picking 新增 stock_id 及联合索引

- wms_store_warehouse: 新增 initiator_subject_id 记录发起方主体ID

- wms_picking: 新增 stock_id 字段 + idx_wms_picking_warehouse_stock 联合索引 + 历史数据回填

- Model 层同步更新关联定义
This commit is contained in:
liufumingren 2026-07-09 03:36:03 +08:00
parent 19f0449992
commit 14a5f13ad5
4 changed files with 81 additions and 1 deletions

View File

@ -31,6 +31,11 @@ module.exports = app => {
allowNull: true,
comment: '库存记录ID',
},
stock_id: {
type: BIGINT(20),
allowNull: true,
comment: '商品SKU ID',
},
quantity: {
type: INTEGER(10).UNSIGNED,
allowNull: false,
@ -102,11 +107,15 @@ module.exports = app => {
name: 'idx_wms_picking_order_item_space',
fields: [ 'order_item_id', 'space_id' ],
},
{
name: 'idx_wms_picking_warehouse_stock',
fields: [ 'warehouse_id', 'stock_id' ],
},
],
});
Picking.associate = function() {
const { OrderItems, Storage, LocationSpace } = app.model;
const { OrderItems, Storage, LocationSpace, StockWarehouse } = app.model;
if (OrderItems) {
Picking.belongsTo(OrderItems, {
@ -131,6 +140,14 @@ module.exports = app => {
constraints: false,
});
}
if (StockWarehouse) {
Picking.belongsTo(StockWarehouse, {
foreignKey: 'stock_id',
as: 'stock',
constraints: false,
});
}
};
return Picking;

View File

@ -509,6 +509,11 @@ module.exports = app => {
allowNull: true,
comment: '解除人',
},
initiator_subject_id: {
type: BIGINT(20).UNSIGNED,
allowNull: true,
comment: '发起方主体ID(wms_store.id)',
},
created_at: {
type: DATE,
allowNull: false,

View File

@ -0,0 +1,24 @@
'use strict';
/**
* wms_store_warehouse 增加 initiator_subject_id 记录发起方主体 ID,
* 用于区分签-约发起方(与 applied_by 账号 ID 互补)。
*/
module.exports = {
async up(queryInterface) {
const { sequelize } = queryInterface;
await sequelize.query(`
ALTER TABLE wms_store_warehouse
ADD COLUMN initiator_subject_id BIGINT(20) UNSIGNED NULL COMMENT '发起方主体ID(wms_store.id)'
AFTER ended_by
`);
},
async down(queryInterface) {
const { sequelize } = queryInterface;
await sequelize.query(`
ALTER TABLE wms_store_warehouse
DROP COLUMN initiator_subject_id
`);
},
};

View File

@ -0,0 +1,34 @@
'use strict';
/**
* 为 wms_picking 表新增 stock_id 字段及联合索引。
*
* 用途:消除面单打印自定义区对 wms_order_items 明细表的强依赖,
* 使打印链路可通过 wms_picking.stock_id 直读商品信息与条码数据。
*/
module.exports = {
up: async queryInterface => {
await queryInterface.sequelize.query(`
ALTER TABLE wms_picking
ADD COLUMN stock_id BIGINT(20) DEFAULT NULL COMMENT '商品SKU ID' AFTER storage_id,
ADD INDEX idx_wms_picking_warehouse_stock (warehouse_id, stock_id)
`);
// 回填已有 picking 记录的 stock_id(通过 storage 链路补全)
await queryInterface.sequelize.query(`
UPDATE wms_picking p
INNER JOIN wms_storage s ON p.storage_id = s.id
SET p.stock_id = s.stock_id
WHERE p.stock_id IS NULL
AND p.storage_id IS NOT NULL
`);
},
down: async queryInterface => {
await queryInterface.sequelize.query(`
ALTER TABLE wms_picking
DROP INDEX idx_wms_picking_warehouse_stock,
DROP COLUMN stock_id
`);
},
};