将一个到多个表的连接右侧转换为 knex 中的数组
convert right side of join of one to many tables into an array in knex
请帮忙
我有 table 个类似这样的想法
ideas = {
id,
title,
abstract,
background,
classification,
technicalfeild,
tag,
user_id,
}
和一个 table 想法图像看起来像这样
idea-images = {
id,
idea_id,
banner,
image_details
}
它们是一对多的关系。我需要 select 所有具有 public 标签的想法,并在想法图像 table 中找到图像,并将图像作为数组附加到想法,因为想法可以有很多图像.
这是我的代码
return knex('ideas')
.where('tag', 'published')
.rightJoin('idea-images', 'ideas.id', '=', 'idea-images.idea_id')
.orderBy('ideas.created_at', 'desc');
这里会是什么 return
[
{
id: 1,
title: 'blahhh ',
abstract: ' blahh',
background: 'blahhh',
classification: 'consumer product',
technicalfeild: '',
description: 'blah....... ',
summary: '',
claims: 'blahhhh',
tag: 'published',
user_id: 3,
created_at: 2020-02-21T00:10:43.692Z,
updated_at: 2020-02-21T00:10:43.692Z,
idea_id: 2,
banner: 'Drawing-2',
image_details: 'blahhh',
},
{
id: 3,
title: 'blahhh ',
abstract: ' test',
background: 'test',
classification: 'blahhhh',
technicalfeild: '',
description: 'test ',
summary: '',
claims: 'test',
tag: 'published',
user_id: 2,
created_at: 2020-02-21T00:10:43.692Z,
updated_at: 2020-02-21T00:10:43.692Z,
idea_id: 3,
banner: 'test',
image_details: 'test',
},
{
id: 4,
title: 'My new car ',
abstract: ' test',
background: 'test',
classification: 'consumer product',
technicalfeild: '',
description: 'test ',
summary: '',
claims: 'test',
tag: 'published',
user_id: 2,
created_at: 2020-02-21T00:10:43.692Z,
updated_at: 2020-02-21T00:10:43.692Z,
idea_id: 3,
banner: 'test2',
image_details: 'test2',
}
]
出于某种原因,如果创意有 2 张图片,它会创建一个新创意!!!我真的不知道如何将图像数据制作成数组。
有 2 种可能的解决方案,
使用group_concat,适用于每个idea图片量小的时候,query如下
https://www.db-fiddle.com/f/74qcRt3siGF2sxT4ykqh9t/1
拆分为 2 个查询,获取想法,然后按相关想法获取所有图像
const ideas = await knex('ideas')
.where('tag', 'published')
.orderBy('ideas.created_at', 'desc');
const ideaImages = await knex('ideaImages').whereIn('ideaId', ideas.map(idea => idea.id));
mergeData(ideas, ideaImages);
请帮忙 我有 table 个类似这样的想法
ideas = {
id,
title,
abstract,
background,
classification,
technicalfeild,
tag,
user_id,
}
和一个 table 想法图像看起来像这样
idea-images = {
id,
idea_id,
banner,
image_details
}
它们是一对多的关系。我需要 select 所有具有 public 标签的想法,并在想法图像 table 中找到图像,并将图像作为数组附加到想法,因为想法可以有很多图像. 这是我的代码
return knex('ideas')
.where('tag', 'published')
.rightJoin('idea-images', 'ideas.id', '=', 'idea-images.idea_id')
.orderBy('ideas.created_at', 'desc');
这里会是什么 return
[
{
id: 1,
title: 'blahhh ',
abstract: ' blahh',
background: 'blahhh',
classification: 'consumer product',
technicalfeild: '',
description: 'blah....... ',
summary: '',
claims: 'blahhhh',
tag: 'published',
user_id: 3,
created_at: 2020-02-21T00:10:43.692Z,
updated_at: 2020-02-21T00:10:43.692Z,
idea_id: 2,
banner: 'Drawing-2',
image_details: 'blahhh',
},
{
id: 3,
title: 'blahhh ',
abstract: ' test',
background: 'test',
classification: 'blahhhh',
technicalfeild: '',
description: 'test ',
summary: '',
claims: 'test',
tag: 'published',
user_id: 2,
created_at: 2020-02-21T00:10:43.692Z,
updated_at: 2020-02-21T00:10:43.692Z,
idea_id: 3,
banner: 'test',
image_details: 'test',
},
{
id: 4,
title: 'My new car ',
abstract: ' test',
background: 'test',
classification: 'consumer product',
technicalfeild: '',
description: 'test ',
summary: '',
claims: 'test',
tag: 'published',
user_id: 2,
created_at: 2020-02-21T00:10:43.692Z,
updated_at: 2020-02-21T00:10:43.692Z,
idea_id: 3,
banner: 'test2',
image_details: 'test2',
}
]
出于某种原因,如果创意有 2 张图片,它会创建一个新创意!!!我真的不知道如何将图像数据制作成数组。
有 2 种可能的解决方案,
使用group_concat,适用于每个idea图片量小的时候,query如下 https://www.db-fiddle.com/f/74qcRt3siGF2sxT4ykqh9t/1
拆分为 2 个查询,获取想法,然后按相关想法获取所有图像
const ideas = await knex('ideas')
.where('tag', 'published')
.orderBy('ideas.created_at', 'desc');
const ideaImages = await knex('ideaImages').whereIn('ideaId', ideas.map(idea => idea.id));
mergeData(ideas, ideaImages);