You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

为何Sequelize会将OID属性转换为`o_i_d`?

解决Sequelize将OID字段转换为o_i_d的问题

问题核心是Sequelize的自动字段名转换机制会把全大写的OID拆分为o_i_d,导致SQL请求找不到数据库中实际存在的OID字段。无需修改数据库或其他关联程序,只需给字段指定明确的数据库映射即可解决:

修改OrderInfo实体的字段定义

在OID字段的配置中添加field: 'OID',强制Sequelize使用指定的数据库字段名,避免自动转换:

const { Model, DataTypes } = require('sequelize');
const sequelize = require('../db/db');

class OrderInfo extends Model {}

OrderInfo.init({
  OID: {
    type: DataTypes.INTEGER,
    primaryKey: true,
    field: 'OID' // 新增该行,绑定数据库实际字段名
  },
  id_client: {
    type: DataTypes.INTEGER,
    allowNull: false,
  },
  id_commande: {
    type: DataTypes.STRING,
    allowNull: false,
  },
  date: {
    type: DataTypes.STRING,
    allowNull: false,
  },
}, {
  sequelize,
  createdAt: 'date',
  tableName: 'commande_info',
});

module.exports = OrderInfo;

额外提示

如果OID是数据库自增主键,建议在字段配置中补充autoIncrement: true,确保Sequelize能正确处理主键生成:

OID: {
  type: DataTypes.INTEGER,
  primaryKey: true,
  field: 'OID',
  autoIncrement: true // 若数据库端设置了自增,添加该行
},

修改后重新执行创建逻辑,Sequelize会直接使用OID字段生成SQL,不会再转换为o_i_d。

内容的提问来源于stack exchange,提问作者isidore

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.06 20:15:40