如何防止 Sequelize 添加 'Id' 到列名?
How to prevent Sequelize adding 'Id' to column name?
当我使用 Sequelize 进行查询时,我发现 'Id' 被添加到我的列名称的末尾,但我不确定如何指示 Sequelize 不这样做?
我为Sequelize创建了实体数据模型,如下:
function initializeDataModel(sequelize) {
var dataModel = { };
dataModel.Playlist = this.sequelize.define('playlist', {
name: Sequelize.STRING,
});
dataModel.PlaylistEntry = this.sequelize.define('playlist_entry', {
playlist: {
name: 'playlist',
type: Sequelize.INTEGER,
references: {
// This is a reference to another model
model: dataModel.Playlist,
// This is the column name of the referenced model
key: 'id'
// This declares when to check the foreign key constraint. PostgreSQL only.
//deferrable: Sequelize.Deferrable.INITIALLY_IMMEDIATE
}
},
track: Sequelize.INTEGER
});
dataModel.PlaylistEntry.belongsTo(
dataModel.Playlist,
{ as: 'Playlist', foreignKey: { name: 'playlist' }});
dataModel.Playlist.hasMany(dataModel.PlaylistEntry);
return dataModel;
}
'playlist_entry'table(在MariaDB中)的字段如下:
- id: INT(11)
- 播放列表:INT(11)
- 轨道:INT(11)
我正在执行的查询如下:
eagerIncludes.push(this.dataModel.PlaylistEntry);
this.dataModel.Playlist.find({
where: { id: playlistId },
limit: limit,
offset: offset,
include: eagerIncludes
}).then(function (results) {
callback(results, options, undefined);
}).catch(function (error) {
callback(undefined, options, error);
});
这导致:
ER_BAD_FIELD_ERROR: Unknown column 'playlist_entries.playlistId' in 'field list'
如有任何建议,我们将不胜感激。更改数据库中的列名不是一个选项。
请注意,这是在尝试使用“eager includes”时出现的问题。
根据一些实验,解决方法是将 'foreignKey' 属性 添加到 hasMany 定义中:
entities.Playlist.hasMany(entities.PlaylistEntry, { foreignKey: 'playlist' });
当我使用 Sequelize 进行查询时,我发现 'Id' 被添加到我的列名称的末尾,但我不确定如何指示 Sequelize 不这样做?
我为Sequelize创建了实体数据模型,如下:
function initializeDataModel(sequelize) {
var dataModel = { };
dataModel.Playlist = this.sequelize.define('playlist', {
name: Sequelize.STRING,
});
dataModel.PlaylistEntry = this.sequelize.define('playlist_entry', {
playlist: {
name: 'playlist',
type: Sequelize.INTEGER,
references: {
// This is a reference to another model
model: dataModel.Playlist,
// This is the column name of the referenced model
key: 'id'
// This declares when to check the foreign key constraint. PostgreSQL only.
//deferrable: Sequelize.Deferrable.INITIALLY_IMMEDIATE
}
},
track: Sequelize.INTEGER
});
dataModel.PlaylistEntry.belongsTo(
dataModel.Playlist,
{ as: 'Playlist', foreignKey: { name: 'playlist' }});
dataModel.Playlist.hasMany(dataModel.PlaylistEntry);
return dataModel;
}
'playlist_entry'table(在MariaDB中)的字段如下:
- id: INT(11)
- 播放列表:INT(11)
- 轨道:INT(11)
我正在执行的查询如下:
eagerIncludes.push(this.dataModel.PlaylistEntry);
this.dataModel.Playlist.find({
where: { id: playlistId },
limit: limit,
offset: offset,
include: eagerIncludes
}).then(function (results) {
callback(results, options, undefined);
}).catch(function (error) {
callback(undefined, options, error);
});
这导致:
ER_BAD_FIELD_ERROR: Unknown column 'playlist_entries.playlistId' in 'field list'
如有任何建议,我们将不胜感激。更改数据库中的列名不是一个选项。
请注意,这是在尝试使用“eager includes”时出现的问题。
根据一些实验,解决方法是将 'foreignKey' 属性 添加到 hasMany 定义中:
entities.Playlist.hasMany(entities.PlaylistEntry, { foreignKey: 'playlist' });