请帮我把桌子设计成这样
ideas = {
id,
title,
abstract,
background,
classification,
technicalfeild,
tag,
user_id,
}还有一个桌子上的想法--像这样的图片
idea-images = {
id,
idea_id,
banner,
image_details
}他们有一对多的关系。我需要选择所有的想法,有公共标签,并找到图像中的想法-图像表和附加图像作为一个数组的想法,因为想法可以有许多图像。这是我的密码
return knex('ideas')
.where('tag', 'published')
.rightJoin('idea-images', 'ideas.id', '=', 'idea-images.idea_id')
.orderBy('ideas.created_at', 'desc');在这里,它会回来
[
{
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个图像!我真的不知道如何把图像数据变成数组。
发布于 2020-02-23 08:22:58
有两个可能的解决方案,
使用https://www.db-fiddle.com/f/74qcRt3siGF2sxT4ykqh9t/1
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);https://stackoverflow.com/questions/60331790
复制相似问题