'use strict'; const TABLE_NAME = 'wms_entry_receiving_binding'; module.exports = { up: async (queryInterface, Sequelize) => { const tables = await queryInterface.showAllTables(); const exists = tables.some(t => (typeof t === 'string' ? t : t.tableName) === TABLE_NAME); if (exists) return; await queryInterface.createTable(TABLE_NAME, { id: { type: Sequelize.BIGINT(20).UNSIGNED, primaryKey: true, autoIncrement: true, allowNull: false, comment: '主键ID', }, entry_item_id: { type: Sequelize.BIGINT(20).UNSIGNED, allowNull: false, comment: '入库明细ID', }, receiving_detail_id: { type: Sequelize.INTEGER, allowNull: false, comment: '收货明细ID', }, allocated_quantity: { type: Sequelize.INTEGER, allowNull: false, comment: '预分配数量', }, status: { type: Sequelize.ENUM('pending', 'committed', 'released'), allowNull: false, defaultValue: 'pending', comment: '绑定状态:pending-待提交 committed-已提交 released-已释放', }, creator_id: { type: Sequelize.BIGINT(20).UNSIGNED, allowNull: true, comment: '创建人ID', }, created_at: { type: Sequelize.DATE, allowNull: false, defaultValue: Sequelize.literal('CURRENT_TIMESTAMP'), comment: '创建时间', }, updated_at: { type: Sequelize.DATE, allowNull: false, defaultValue: Sequelize.literal('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'), comment: '更新时间', }, deleted_at: { type: Sequelize.DATE, allowNull: true, defaultValue: null, comment: '软删除时间', }, }); await queryInterface.addIndex(TABLE_NAME, [ 'entry_item_id' ], { name: 'idx_entry_item_id', }); await queryInterface.addIndex(TABLE_NAME, [ 'receiving_detail_id' ], { name: 'idx_receiving_detail_id', }); await queryInterface.addIndex(TABLE_NAME, [ 'status' ], { name: 'idx_status', }); await queryInterface.addIndex(TABLE_NAME, [ 'entry_item_id', 'receiving_detail_id', 'status' ], { unique: true, name: 'uk_entry_receiving', }); }, down: async queryInterface => { await queryInterface.dropTable(TABLE_NAME); }, };