// app/model/file_reference.js 'use strict'; module.exports = app => { const { BIGINT, INTEGER, STRING, DATE } = app.Sequelize; const FileReference = app.model.define('wms_file_reference', { id: { type: BIGINT.UNSIGNED, primaryKey: true, autoIncrement: true, comment: '主键ID', }, file_id: { type: BIGINT.UNSIGNED, allowNull: false, comment: '文件ID', references: { model: 'wms_file', key: 'id', }, }, warehouse_id: { type: BIGINT.UNSIGNED, allowNull: false, comment: '仓库ID(冗余,优化查询)', references: { model: 'wms_store', key: 'id', }, }, business_type: { type: STRING(50), allowNull: false, comment: '业务类型:sku_image/packing_list/inspection_report', }, business_id: { type: STRING(64), allowNull: false, comment: '业务ID(如SKU编码、出库单号)', }, display_order: { type: INTEGER.UNSIGNED, defaultValue: 0, comment: '显示顺序', }, created_by: { type: STRING(64), allowNull: false, comment: '关联创建者', }, created_at: { type: DATE, allowNull: false, defaultValue: app.Sequelize.literal('CURRENT_TIMESTAMP'), comment: '创建时间', }, }, { tableName: 'wms_file_reference', comment: 'WMS文件业务关联表', timestamps: false, // 使用自定义的created_at indexes: [ { name: 'uk_business_file', unique: true, fields: [ 'business_module', 'business_type', 'business_id', 'file_id' ], }, { name: 'idx_file_id', fields: [ 'file_id' ], }, { name: 'idx_warehouse_business', fields: [ 'warehouse_id', 'business_module', 'business_type' ], }, { name: 'idx_business', fields: [ 'business_module', 'business_id' ], }, ], }); // 关联定义 FileReference.associate = function() { const { File, Store } = app.model; // 属于一个文件 FileReference.belongsTo(File, { foreignKey: 'file_id', as: 'file', }); // 属于一个仓库 FileReference.belongsTo(Store, { foreignKey: 'warehouse_id', as: 'warehouse', }); }; return FileReference; };