排除关联的id列

时间:2017-05-18 15:12:01

标签: sql-server sequelize.js

我在Sequelize ORM中有一个关联设置为:
process.belongsTo(binary, {foreignKey: 'binary_id'});process模型之间的binary。我使用代码查询process模型:

process.findAndCountAll({
    include: [{
        model: binary,
        attributes: ["name", "version", "company"]
    }],
    attributes: ["id", "command"],
}).then(function (result) {
    console.log(result);
});

由此生成的SQL查询是:

SELECT 
  [process].[id], 
  [process].[command], 
  [binary].[id] AS [binary.id], 
  [binary].[name] AS [binary.name], 
  [binary].[version] AS [binary.version], 
  [binary].[company] AS [binary.company] 
FROM [process] AS [process] LEFT OUTER JOIN [binary] AS [binary] ON 
     [process].[binary_id] = [binary].[id];

正如您所看到的,它包括一个额外的列binary.id,即使我在attributes: []中没有提到过。

我还尝试按照here

添加through参数
process.findAndCountAll({
    include: [{
        model: binary,
        attributes: ["name", "version", "company"],
        through: {
            attributes: []
        }
    }],
    attributes: ["id", "command"],
}).then(function (result) {
    console.log(result);
});

但是我得到以下错误:

  

未处理拒绝TypeError:无法读取未定义

的属性'getTableName'

那么,有没有办法阻止它被添加?或者排除id模型的binary列? 我正在使用MSSQL。

1 个答案:

答案 0 :(得分:0)

尝试一下:

process.findAndCountAll({
    include: [{
        model: binary,
        attributes: ["name", "version", "company"],
        through: {
        attributes: []
        }
    }],
    attributes: ["id", "command"],
}).then(function (result) {
    console.log(result);
});

Source