使用 Sequelize 6,我有一个与表相关联的模型,该模型在某些列中有大写字母,在其他列中有大写字母(这些列不是 created_at 或 updated_at)。
下划线的模型配置似乎影响与相应表列关联的每个属性。有没有办法覆盖模型级别的下划线配置,使得与表列关联的属性保留表的原始列名?
代码示例:
const someModel = sequelize.define('some_table', {
'id': {
type: dataTypes.BIGINT,
primaryKey: true,
autoIncrement: true,
},
'other_table_id': {
type: dataTypes.BIGINT,
allowNull: false,
references: {
model: 'other_table',
key: 'id',
deferrable: INITIALLY_IMMEDIATE,
},
},
'CAPITAL_letterColumn': {
type: dataTypes.STRING,
allowNull: true,
defaultValue: null,,
}
}, {
timestamps: false,
paranoid: false,
// underscored: true, // true === snake case, false === camelcase
}
});
someModel.associate = (models) => {
someModel.belongsTo(models.other_table, { foreignKey: 'other_table_id', targetKey: 'id' });
};
我收到以下错误:
UnhandledPromiseRejectionWarning: SequelizeDatabaseError: column "otherTableId" does not exist
如果我设置
underscored: true
我收到以下错误:
UnhandledPromiseRejectionWarning: SequelizeDatabaseError: column "c_a_p_i_t_a_l_letter_column" of relation "some_table" does not exist
答案 0 :(得分:0)
看起来在模型定义中添加字段属性解决了这个问题。
'other_table_id': {
field: 'other_table_id', // <-- this line
type: dataTypes.BIGINT,
allowNull: false,
references: {
model: 'other_table',
key: 'id',
deferrable: INITIALLY_IMMEDIATE,
},
},