gpt4 book ai didi

node.js - 通过 Sequelize 中的属性排除

转载 作者:行者123 更新时间:2023-12-03 22:17:38 24 4
gpt4 key购买 nike

我有 2 个表帖子和标签。我正在使用 Tag 来获取与之相关的所有帖子。

models.Tag.findAll({
attributes: ['tagName'],
include: [
{model: models.Post
attributes: ['content']
through: {
attributes: []
}
}
]
})

问题是它选择了查询中的所有直通表属性。

虽然执行 include.through.attributes = [] 属性没有显示在结果查询中,但是当我 console.log select 查询时,它仍然选择直通表的所有属性。

有没有排除直通表?它使 Postgres 中的 groupBy 变得不可能,因为它会自动选择所有列。

最佳答案

我不会在 sequelize@6.5.1 sqlite3@5.0.2 上复制:

#!/usr/bin/env node

// Find all posts by users that a given user follows.
// https://stackoverflow.com/questions/42632943/sequelize-multiple-where-clause

const assert = require('assert');
const path = require('path');

const { Sequelize, DataTypes } = require('sequelize');

const sequelize = new Sequelize({
dialect: 'sqlite',
storage: 'tmp.' + path.basename(__filename) + '.sqlite',
});

(async () => {

// Create the tables.
const User = sequelize.define('User', {
name: { type: DataTypes.STRING },
});
const Post = sequelize.define('Post', {
body: { type: DataTypes.STRING },
});
User.belongsToMany(User, {through: 'UserFollowUser', as: 'Follows'});
User.hasMany(Post);
Post.belongsTo(User);
await sequelize.sync({force: true});

// Create data.
const users = await User.bulkCreate([
{name: 'user0'},
{name: 'user1'},
{name: 'user2'},
{name: 'user3'},
])

const posts = await Post.bulkCreate([
{body: 'body00', UserId: users[0].id},
{body: 'body01', UserId: users[0].id},
{body: 'body10', UserId: users[1].id},
{body: 'body11', UserId: users[1].id},
{body: 'body20', UserId: users[2].id},
{body: 'body21', UserId: users[2].id},
{body: 'body30', UserId: users[3].id},
{body: 'body31', UserId: users[3].id},
])

await users[0].addFollows([users[1], users[2]])

const user0Follows = await User.findByPk(users[0].id, {
attributes: [
[Sequelize.fn('COUNT', Sequelize.col('Follows.Posts.id')), 'count']
],
include: [
{
model: User,
as: 'Follows',
attributes: [],
//through: { attributes: [] },
include: [
{
model: Post,
attributes: [],
}
],
},
],
})
assert.strictEqual(user0Follows.dataValues.count, 4);

await sequelize.close();
})();
美化生成的 SELECT 是:
SELECT
`User`.`id`,
COUNT(`Follows->Posts`.`id`) AS `count`
FROM
`Users` AS `User`
LEFT OUTER JOIN `UserFollowUser` AS `Follows->UserFollowUser` ON `User`.`id` = `Follows->UserFollowUser`.`UserId`
LEFT OUTER JOIN `Users` AS `Follows` ON `Follows`.`id` = `Follows->UserFollowUser`.`FollowId`
LEFT OUTER JOIN `Posts` AS `Follows->Posts` ON `Follows`.`id` = `Follows->Posts`.`UserId`
WHERE
`User`.`id` = 1;
如果我删除 through: { attributes: [] }, 则出现 through 属性,因此该语句正在按预期执行某些操作:
SELECT
`User`.`id`,
COUNT(`Follows->Posts`.`id`) AS `count`,
`Follows->UserFollowUser`.`createdAt` AS `Follows.UserFollowUser.createdAt`,
`Follows->UserFollowUser`.`updatedAt` AS `Follows.UserFollowUser.updatedAt`,
`Follows->UserFollowUser`.`UserId` AS `Follows.UserFollowUser.UserId`,
`Follows->UserFollowUser`.`FollowId` AS `Follows.UserFollowUser.FollowId`
FROM
`Users` AS `User`
LEFT OUTER JOIN `UserFollowUser` AS `Follows->UserFollowUser` ON `User`.`id` = `Follows->UserFollowUser`.`UserId`
LEFT OUTER JOIN `Users` AS `Follows` ON `Follows`.`id` = `Follows->UserFollowUser`.`FollowId`
LEFT OUTER JOIN `Posts` AS `Follows->Posts` ON `Follows`.`id` = `Follows->Posts`.`UserId`
WHERE
`User`.`id` = 1;
所以很可能这是固定的。

关于node.js - 通过 Sequelize 中的属性排除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42735347/

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