跨多个表创建 Sequelize 查询、多个内部连接和 Wheres
Create Sequelize Query, Multiple Innerjoins and Wheres across multiple tables
我创建了一个成功的 mysql 查询,该查询连接了 2 个彼此具有外键的表。除了让用户附加到响应之外,我在 Sequelize 中几乎完成了所有工作。我的查询成功了,但我不明白如何获取它。
这是我的 mysql 查询,它专门获取与响应“user_id”
具有相同 ID 的“用户”
SELECT *
FROM meetup_db.posts p
INNER JOIN meetup_db.responses r
INNER JOIN meetup_db.users u
WHERE p.id = r.post_id AND
r.user_id = u.id
这是我的 sequelize 查询,几乎可以工作,但不会在 user_id = id
的每个响应中返回用户
const dbPostData = await Posts.findOne({
where: { id: req.params.id },
include: [
{ model: Users },
{
model: Responses,
include: {
model: Users,
where: { id: Responses.user_id }, //this is the line in question i need the user that has the same id as the Response.user_id
}
}
]
});
sequelize 正确输出除用户以外的所有内容,这是一个示例输出,您可以看到响应中列出了 Users,但用户仅显示为 [对象]
{
id: 1,
title: 'BBQ At My House!!',
description: 'Hey gang! Im having a BBQ at my house! I hope all can attend!!',
upvotes: 44,
location: '2113 Main St. Austin, TX.',
date_occuring: 2021-08-04T18:00:00.000Z,
created_at: 2021-08-06T04:42:01.000Z,
edited: false,
user_id: 1,
createdAt: 2021-08-06T04:42:01.000Z,
updatedAt: 2021-08-06T04:42:01.000Z,
User: {
id: 1,
username: 'Jennifer Wylan',
email: 'jwylan@gmail.com',
password: 'ewfchijwnsj',
image_url: '/assets/fake-pfp/fakeperson1.png'
},
Responses: [
{
id: 4,
response: 'Im in there like swimwear!',
user_id: 4,
post_id: 1,
createdAt: 2021-08-06T04:42:01.000Z,
updatedAt: 2021-08-06T04:42:01.000Z,
User: [Object] //these lines right here need to look like the above User: {} Object
},
{
id: 5,
response: 'weeeeeeeeeeeeeeeeeeeeeeeeeeeeee',
user_id: 1,
post_id: 1,
createdAt: 2021-08-06T04:42:01.000Z,
updatedAt: 2021-08-06T04:42:01.000Z,
User: [Object] //these lines right here need to look like the above User: {} Object
}
],
}
我明白了。所以它已经起作用了。
在我的终端中,响应的输出将用户密钥显示为 [Object]
...原来当它像 3 个表深时,终端只显示 [Object] 但它实际上已经在那里,你也不需要 where:
因为它已经在寻找外键
我创建了一个成功的 mysql 查询,该查询连接了 2 个彼此具有外键的表。除了让用户附加到响应之外,我在 Sequelize 中几乎完成了所有工作。我的查询成功了,但我不明白如何获取它。
这是我的 mysql 查询,它专门获取与响应“user_id”
具有相同 ID 的“用户”SELECT *
FROM meetup_db.posts p
INNER JOIN meetup_db.responses r
INNER JOIN meetup_db.users u
WHERE p.id = r.post_id AND
r.user_id = u.id
这是我的 sequelize 查询,几乎可以工作,但不会在 user_id = id
的每个响应中返回用户const dbPostData = await Posts.findOne({
where: { id: req.params.id },
include: [
{ model: Users },
{
model: Responses,
include: {
model: Users,
where: { id: Responses.user_id }, //this is the line in question i need the user that has the same id as the Response.user_id
}
}
]
});
sequelize 正确输出除用户以外的所有内容,这是一个示例输出,您可以看到响应中列出了 Users,但用户仅显示为 [对象]
{
id: 1,
title: 'BBQ At My House!!',
description: 'Hey gang! Im having a BBQ at my house! I hope all can attend!!',
upvotes: 44,
location: '2113 Main St. Austin, TX.',
date_occuring: 2021-08-04T18:00:00.000Z,
created_at: 2021-08-06T04:42:01.000Z,
edited: false,
user_id: 1,
createdAt: 2021-08-06T04:42:01.000Z,
updatedAt: 2021-08-06T04:42:01.000Z,
User: {
id: 1,
username: 'Jennifer Wylan',
email: 'jwylan@gmail.com',
password: 'ewfchijwnsj',
image_url: '/assets/fake-pfp/fakeperson1.png'
},
Responses: [
{
id: 4,
response: 'Im in there like swimwear!',
user_id: 4,
post_id: 1,
createdAt: 2021-08-06T04:42:01.000Z,
updatedAt: 2021-08-06T04:42:01.000Z,
User: [Object] //these lines right here need to look like the above User: {} Object
},
{
id: 5,
response: 'weeeeeeeeeeeeeeeeeeeeeeeeeeeeee',
user_id: 1,
post_id: 1,
createdAt: 2021-08-06T04:42:01.000Z,
updatedAt: 2021-08-06T04:42:01.000Z,
User: [Object] //these lines right here need to look like the above User: {} Object
}
],
}
我明白了。所以它已经起作用了。
在我的终端中,响应的输出将用户密钥显示为 [Object]
...原来当它像 3 个表深时,终端只显示 [Object] 但它实际上已经在那里,你也不需要 where:
因为它已经在寻找外键