gpt4 book ai didi

javascript - 在 sequelize js 中获取关系的值(value)

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

我正在尝试获取该值并返回 postgres 我该列不存在的信息。我无法以任何方式处理。

"column Group.participant.id does not exist"



关系:
User.belongsToMany(models.group, { as: 'Member', through: { model: models.participant, unique: false}, foreignKey: 'userId', constraints: false});

Group.belongsToMany(models.user, { as: 'Group', through: { model: models.participant, unique: false}, foreignKey: 'groupId', constraints: false });

Participant.belongsTo(models.user, {
foreignKey: "userId", as: "Member"
});

Participant.belongsTo(models.group, {
foreignKey: "groupId", as: "Group"
});

查询/Sequelizejs:
const User = app.db_connect.postgres.models.user;

Group.findAll({
include: [
{ model: User,
as: 'Group',
where: {
"participant.id": idUser
}
// attributes: ['name']
}
]})
.then(result => res.json(result))
.catch(error => {
res.status(412).json({msg: error.message});
});

});

查询生成:
Executing (default): SELECT "group"."id", "group"."name", "group"."isMain", "group"."privacy", "group"."description", "group"."img", "group"."createdAt", "group"."updatedAt", "group"."destroyTime", "group"."categoryId", "Group"."id" AS "Group.id", "Group"."name" AS "Group.name", "Group"."birthday" AS "Group.birthday", "Group"."cpf" AS "Group.cpf", "Group"."email" AS "Group.email", "Group"."img" AS "Group.img", "Group"."status" AS "Group.status", "Group"."phone" AS "Group.phone", "Group"."cellPhone" AS "Group.cellPhone", "Group"."nickName" AS "Group.nickName", "Group"."password" AS "Group.password", "Group"."found" AS "Group.found", "Group"."createdAt" AS "Group.createdAt", "Group"."updatedAt" AS "Group.updatedAt", "Group"."destroyTime" AS "Group.destroyTime", "Group.participant"."id" AS "Group.participant.id", "Group.participant"."function" AS "Group.participant.function", "Group.participant"."createdAt" AS "Group.participant.createdAt", "Group.participant"."updatedAt" AS "Group.participant.updatedAt", "Group.participant"."groupId" AS "Group.participant.groupId", "Group.participant"."userId" AS "Group.participant.userId" FROM "groups" AS "group" INNER JOIN ("participants" AS "Group.participant" INNER JOIN "users" AS "Group" ON "Group"."id" = "Group.participant"."userId") ON "group"."id" = "Group.participant"."groupId" AND ("Group"."destroyTime" IS NULL AND "Group"."participant.id" = 1) WHERE "group"."destroyTime" IS NULL;

不带 where 返回: Pastebin

最佳答案

id 中只使用 where ,因为在包含部分您使用特定模型的列

Group.findAll({
include: [{ model: User, as: 'Group',
where: {
"id": idUser
}
}]})

关于javascript - 在 sequelize js 中获取关系的值(value),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38531308/

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