[英]How to mix sequelize camel case and snake case?
使用 Sequelize 6,我有一個 model,與表相關聯,在某些列中具有大寫字母,在其他列中具有蛇形大小寫(這些列不是 created_at 或 updated_at)。
帶下划線的 model 配置似乎會影響與相應表列關聯的每個屬性。 有沒有辦法覆蓋 model 級別下划線配置,以便與表列關聯的屬性保留表的原始列名?
代碼示例:
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
看起來像在 model 定義中添加字段屬性解決了這個問題。
'other_table_id': {
field: 'other_table_id', // <-- this line
type: dataTypes.BIGINT,
allowNull: false,
references: {
model: 'other_table',
key: 'id',
deferrable: INITIALLY_IMMEDIATE,
},
},
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.