如何防止Sequelize添加' Id'到列名?

时间:2016-05-09 21:36:50

标签: javascript mysql node.js sequelize.js

我发现当我使用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;
}    

播放列表中的字段' table(在MariaDB中)如下:

  • id:INT(11)
  • 播放列表:INT(11)
  • track: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'时会遇到这个问题。

1 个答案:

答案 0 :(得分:0)

基于一些实验,修复是将'foreignKey'属性添加到hasMany定义:

entities.Playlist.hasMany(entities.PlaylistEntry, { foreignKey: 'playlist' });