oms/backend/database/migrations/20261105090000-normalize-charset-for-dictionary-tables.js
liufumingren 426e6e9e7b fix(express): 自定义快递编码列表因字符集排序规则不一致导致 500
- 列表/详情不再用 Sequelize include 关联 express_companies(该 JOIN 在两边排序规则不同时报
  ER_CANT_AGGREGATE_2COLLATIONS),改为一次 IN 查询后在内存组装,响应仍保留 expressCompany/express_name
- 新增迁移把未显式指定字符集的字典/任务表统一为 utf8mb4 / utf8mb4_unicode_ci
  (custom_express_codes、external_print_express_interfaces、warehouse_express_mappings、
   waybill_fetch_records、order_sync_tasks、shop_database_configs、operation_logs、openapi_request_idempotency)

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-11-05 17:33:27 +08:00

42 lines
1.5 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

'use strict';
/**
* 统一「字典表 / 任务表」的字符集与排序规则
*
* 这些表当初建表时没有显式指定 charset/collate,字符集由数据库默认值决定:
* - 本地/部分环境默认是 utf8mb3(utf8_general_ci)
* - 另一些环境默认是 utf8mb4(utf8mb4_general_ci 或 utf8mb4_0900_ai_ci)
* 而 `express_companies` 等核心字典表固定为 utf8mb4 / utf8mb4_unicode_ci。
*
* 只要两边同字符集但排序规则不同,MySQL 在 JOIN / 比较时直接报
* `ER_CANT_AGGREGATE_2COLLATIONS: Illegal mix of collations`(自定义快递编码列表 500 即由此触发),
* 因此这里把相关表统一转换成 utf8mb4 / utf8mb4_unicode_ci。
*/
const TABLES = [
'custom_express_codes',
'external_print_express_interfaces',
'warehouse_express_mappings',
'waybill_fetch_records',
'order_sync_tasks',
'shop_database_configs',
'operation_logs',
'openapi_request_idempotency',
];
module.exports = {
async up(queryInterface) {
const existTables = new Set(await queryInterface.showAllTables());
for (const table of TABLES) {
if (!existTables.has(table)) continue;
await queryInterface.sequelize.query(
`ALTER TABLE \`${table}\` CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci`
);
}
},
// 字符集统一属于修复性变更(回滚会重新引入排序规则冲突导致 JOIN 报错),故不做反向操作
async down() {
return Promise.resolve();
},
};