gpt4 book ai didi

sql-server - 排除关联的 id 列

转载 作者:行者123 更新时间:2023-12-03 22:41:27 26 4
gpt4 key购买 nike

我在 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: [] 中提到。

我还尝试添加 through 参数,如上所述 here
process.findAndCountAll({
include: [{
model: binary,
attributes: ["name", "version", "company"],
through: {
attributes: []
}
}],
attributes: ["id", "command"],
}).then(function (result) {
console.log(result);
});

但是我收到以下错误:

Unhandled rejection TypeError: Cannot read property 'getTableName' of undefined



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

最佳答案

试试这个:

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

Source

关于sql-server - 排除关联的 id 列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44051605/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com