Join sequelize include只返回一个结果

Join sequelize include只返回一个结果,join,sequelize.js,postgresql-9.4,Join,Sequelize.js,Postgresql 9.4,我有一个Users表和一个Groups表,当我检索单个用户时,我想得到该用户所属的所有组的列表。我创建了一个名为GroupUsers的联接表,上面有userId和groupId,这就是我如何知道用户属于一个组的原因。现在,当我尝试使用find上的sequelizeinclude获取用户所属组的列表时,如果用户属于不同的组,它只返回第一个匹配项。我如何解决这个问题 用户控制器 return models.Users .find({ include: [{ model: models.G

我有一个
Users
表和一个
Groups
表,当我检索单个用户时,我想得到该用户所属的所有组的列表。我创建了一个名为
GroupUsers
的联接表,上面有
userId
groupId
,这就是我如何知道用户属于一个组的原因。现在,当我尝试使用
find
上的sequelize
include
获取用户所属组的列表时,如果用户属于不同的组,它只返回第一个匹配项。我如何解决这个问题

用户控制器

return models.Users
.find({
  include: [{
    model: models.Groups,
    as: 'groups',
    limit: null,
    required: false
  }],
  where: { username }
})
返回以下内容:

{
    "id": 1,
    "username": "John",
    "phone": "xxxxxxxx",
    "email": "john@email.com",
    "createdAt": "2017-07-16T20:14:04.744Z",
    "updatedAt": "2017-07-16T20:14:04.744Z",
    "groups": [
        {
            "id": 1,
            "name": "Test Group",
            "type": "Public",
            "createdAt": "2017-07-16T20:13:18.392Z",
            "updatedAt": "2017-07-16T20:13:18.392Z",
            "GroupUsers": {
                "userId": 1,
                "groupId": 1,
                "last_seen": null,
                "createdAt": "2017-07-16T20:14:27.903Z",
                "updatedAt": "2017-07-16T20:14:27.903Z"
            }
        }
    ]
}
    {
        "id": 1,
        "username": "John",
        "phone": "xxxxxxxx",
        "email": "john@email.com",
        "createdAt": "2017-07-16T20:14:04.744Z",
        "updatedAt": "2017-07-16T20:14:04.744Z",
        "groups": [
            {
                "id": 1,
                "name": "Test Group",
                "type": "Public",
                "createdAt": "2017-07-16T20:13:18.392Z",
                "updatedAt": "2017-07-16T20:13:18.392Z",
                "GroupUsers": {
                    "userId": 1,
                    "groupId": 1,
                    "last_seen": null,
                    "createdAt": "2017-07-16T20:14:27.903Z",
                    "updatedAt": "2017-07-16T20:14:27.903Z"
                }
            },
            {
                "id": 2,
                "name": "Test Group 2",
                "type": "Public",
                "createdAt": "2017-07-16T20:13:18.392Z",
                "updatedAt": "2017-07-16T20:13:18.392Z",
                "GroupUsers": {
                    "userId": 1,
                    "groupId": 2,
                    "last_seen": null,
                    "createdAt": "2017-07-16T20:14:27.903Z",
                    "updatedAt": "2017-07-16T20:14:27.903Z"
                }
            }
        ]
    }
而不是这个:

{
    "id": 1,
    "username": "John",
    "phone": "xxxxxxxx",
    "email": "john@email.com",
    "createdAt": "2017-07-16T20:14:04.744Z",
    "updatedAt": "2017-07-16T20:14:04.744Z",
    "groups": [
        {
            "id": 1,
            "name": "Test Group",
            "type": "Public",
            "createdAt": "2017-07-16T20:13:18.392Z",
            "updatedAt": "2017-07-16T20:13:18.392Z",
            "GroupUsers": {
                "userId": 1,
                "groupId": 1,
                "last_seen": null,
                "createdAt": "2017-07-16T20:14:27.903Z",
                "updatedAt": "2017-07-16T20:14:27.903Z"
            }
        }
    ]
}
    {
        "id": 1,
        "username": "John",
        "phone": "xxxxxxxx",
        "email": "john@email.com",
        "createdAt": "2017-07-16T20:14:04.744Z",
        "updatedAt": "2017-07-16T20:14:04.744Z",
        "groups": [
            {
                "id": 1,
                "name": "Test Group",
                "type": "Public",
                "createdAt": "2017-07-16T20:13:18.392Z",
                "updatedAt": "2017-07-16T20:13:18.392Z",
                "GroupUsers": {
                    "userId": 1,
                    "groupId": 1,
                    "last_seen": null,
                    "createdAt": "2017-07-16T20:14:27.903Z",
                    "updatedAt": "2017-07-16T20:14:27.903Z"
                }
            },
            {
                "id": 2,
                "name": "Test Group 2",
                "type": "Public",
                "createdAt": "2017-07-16T20:13:18.392Z",
                "updatedAt": "2017-07-16T20:13:18.392Z",
                "GroupUsers": {
                    "userId": 1,
                    "groupId": 2,
                    "last_seen": null,
                    "createdAt": "2017-07-16T20:14:27.903Z",
                    "updatedAt": "2017-07-16T20:14:27.903Z"
                }
            }
        ]
    }

我确信我在某个地方做错了什么,我只是不知道在哪里,同样的事情也可能是sequelize的原因,包括结果中的联接表:
GroupUsers

在我的关联中,我似乎做了:

Users.belongsToMany(models.Groups, {
  through: 'GroupUsers',
  as: 'groups',
  foreignKey: 'groupId'
});
而不是:

Users.belongsToMany(models.Groups, {
  through: 'GroupUsers',
  as: 'groups',
  foreignKey: 'userId'
});
注意
foreignKey
属性

对于同样返回的
GroupUsers
对象,我通过以下操作删除了它:

include: [{
    model: models.Groups,
    as: 'groups',
    attributes: ['id', 'name'],
    through: { attributes: [] }
  }]

请注意
键,该键的
属性设置为空数组。

第二个键的foreignKey:'userId'。我写了一篇关于它的文章,您可以查看: