diff --git a/backend/models/Business.js b/backend/models/Business.js index 602f6f5..0a63840 100644 --- a/backend/models/Business.js +++ b/backend/models/Business.js @@ -19,8 +19,11 @@ const Business = sequelize.define( allowNull: true, }, status: { - type: DataTypes.ENUM('active', 'offline'), + type: DataTypes.STRING, defaultValue: 'active', + validate: { + isIn: [['active', 'offline']], + }, }, offlineDate: { type: DataTypes.DATE, diff --git a/backend/models/Warehouse.js b/backend/models/Warehouse.js index 35e81d6..4b04808 100644 --- a/backend/models/Warehouse.js +++ b/backend/models/Warehouse.js @@ -25,8 +25,11 @@ const Warehouse = sequelize.define( defaultValue: 100, }, status: { - type: DataTypes.ENUM('active', 'inactive'), + type: DataTypes.STRING, defaultValue: 'active', + validate: { + isIn: [['active', 'inactive']], + }, }, description: { type: DataTypes.TEXT, diff --git a/backend/scripts/init-database.js b/backend/scripts/init-database.js index 347a0be..c767cdf 100644 --- a/backend/scripts/init-database.js +++ b/backend/scripts/init-database.js @@ -227,27 +227,29 @@ async function initDatabase() { // 同步所有模型(按依赖顺序) console.log('开始同步模型...'); + const alterOptions = process.env.DB_TYPE === 'mysql' ? { alter: true } : {}; + // 基础模型 - await User.sync({ alter: true }); - await Room.sync({ alter: true }); - await Rack.sync({ alter: true }); - await Device.sync({ alter: true }); - await FaultCategory.sync({ alter: true }); + await User.sync(alterOptions); + await Room.sync(alterOptions); + await Rack.sync(alterOptions); + await Device.sync(alterOptions); + await FaultCategory.sync(alterOptions); // 耗材相关模型 - await ConsumableCategory.sync({ alter: true }); - await Consumable.sync({ alter: true }); - await ConsumableLog.sync({ alter: true }); - await ConsumableRecord.sync({ alter: true }); - await ConsumableLogArchive.sync({ alter: true }); + await ConsumableCategory.sync(alterOptions); + await Consumable.sync(alterOptions); + await ConsumableLog.sync(alterOptions); + await ConsumableRecord.sync(alterOptions); + await ConsumableLogArchive.sync(alterOptions); // 盘点相关模型 - await InventoryPlan.sync({ alter: true }); - await InventoryTask.sync({ alter: true }); - await InventoryRecord.sync({ alter: true }); + await InventoryPlan.sync(alterOptions); + await InventoryTask.sync(alterOptions); + await InventoryRecord.sync(alterOptions); // 系统设置 - await SystemSetting.sync({ alter: true }); + await SystemSetting.sync(alterOptions); console.log('数据库表结构同步完成'); diff --git a/backend/server.js b/backend/server.js index b86ecd4..ef3ab31 100644 --- a/backend/server.js +++ b/backend/server.js @@ -6,7 +6,7 @@ ensureJwtSecret(); const express = require('express'); const cors = require('cors'); const fileUpload = require('express-fileupload'); -const { sequelize } = require('./db'); +const { sequelize, dbDialect } = require('./db'); const { FILE_UPLOAD } = require('./config'); const { generateId } = require('./utils/idGenerator'); const logger = require('./utils/logger').module('Server'); @@ -96,10 +96,12 @@ async function syncBusinessModels() { const DeviceBusiness = require('./models/DeviceBusiness'); const Warehouse = require('./models/Warehouse'); - await Business.sync(); - await DeviceBusiness.sync(); - await Warehouse.sync(); - logger.info('业务/设备关联/库房模型同步完成'); + const alterOptions = dbDialect === 'mysql' ? { alter: true } : {}; + + await Business.sync(alterOptions); + await DeviceBusiness.sync(alterOptions); + await Warehouse.sync(alterOptions); + logger.info(`业务/设备关联/库房模型同步完成(${dbDialect}${dbDialect === 'mysql' ? ' alter mode' : ''})`); } async function initDefaultSystemSettings() {