在sequelize js

时间:2016-07-22 16:27:27

标签: javascript json node.js postgresql sequelize.js

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

  

“列Group.participant.id不存在”

关系:

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

的返回

1 个答案:

答案 0 :(得分:0)

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

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