Prisma,来自 table 1 的 select 行,取决于 table 2 中的最新外键
Prisma, select row from table 1, depending on the latest foreign key in table 2
为糟糕的标题道歉,正在努力想另一个。
目前,我有 2 个表,publication
和 publicationStatus
。
出版物看起来像:
{
"id": "ckyil950d00027v2str5ljo7h",
"url_slug": "ckyil950e00037v2s4mqxvaho",
"type": "PEER_REVIEW",
"title": "Intersting review",
"content": "Content is optional at this stage",
"doi": "1093/ajae/aaq063",
"createdBy": "test-user-1",
"createdAt": "2022-01-17T11:12:50.845Z",
"updatedAt": "2022-01-17T11:12:50.847Z",
"publicationStatus": [
{
"status": "LIVE",
"createdAt": "2022-01-19T11:12:50.846Z",
"id": "ckyil950e00047v2sx4urbfte"
},
{
"status": "DRAFT",
"createdAt": "2022-01-17T11:12:50.846Z",
"id": "ckyil950e00047v2sx4urbfth"
}
],
"user": {
"id": "test-user-1",
"firstName": "Test",
"lastName": "User 1"
}
}
其中 publication
与 publicationStatus
具有一对多关系。
我需要做的是 find
查询,如果 latest publicationStatus
只有 returns a publication
因为 publication
,有 LIVE
的 status
。
有什么想法吗?
编辑:
我最接近的是这个 psuedo 代码:
await prisma.publication.findFirst({
where: {
id,
publicationStatus: {
where: {
status: 'LIVE'
},
take: 1,
orderBy: {
createdAt: 'desc'
}
},
}
});
这段代码没有工作,但演示了我正在努力实现的目标。
不幸的是,我认为目前无法直接使用 Prisma 查询来完成您希望的操作。不过,我可以建议两种可能的解决方法:
方法 1: 获取最新的 publicationStatus
以及 publication
并检查节点应用程序中的 status
。
这就是查询的样子:
let publication = await prisma.publication.findFirst({
where: {
id
},
include: {
publicationStatus: {
orderBy: {
createdAt: 'desc'
},
take: 1
}
}
});
// check publication.publicationStatus[0].status and handle appropriately
方法 2: 使用 queryRaw
方法编写原始 SQL 查询。
对于单个出版物,我认为使用方法 1 会更容易。但是,如果您想 return 所有 LIVE 出版物(您在评论),方法 1 的性能特征可能不理想。
为糟糕的标题道歉,正在努力想另一个。
目前,我有 2 个表,publication
和 publicationStatus
。
出版物看起来像:
{
"id": "ckyil950d00027v2str5ljo7h",
"url_slug": "ckyil950e00037v2s4mqxvaho",
"type": "PEER_REVIEW",
"title": "Intersting review",
"content": "Content is optional at this stage",
"doi": "1093/ajae/aaq063",
"createdBy": "test-user-1",
"createdAt": "2022-01-17T11:12:50.845Z",
"updatedAt": "2022-01-17T11:12:50.847Z",
"publicationStatus": [
{
"status": "LIVE",
"createdAt": "2022-01-19T11:12:50.846Z",
"id": "ckyil950e00047v2sx4urbfte"
},
{
"status": "DRAFT",
"createdAt": "2022-01-17T11:12:50.846Z",
"id": "ckyil950e00047v2sx4urbfth"
}
],
"user": {
"id": "test-user-1",
"firstName": "Test",
"lastName": "User 1"
}
}
其中 publication
与 publicationStatus
具有一对多关系。
我需要做的是 find
查询,如果 latest publicationStatus
只有 returns a publication
因为 publication
,有 LIVE
的 status
。
有什么想法吗?
编辑: 我最接近的是这个 psuedo 代码:
await prisma.publication.findFirst({
where: {
id,
publicationStatus: {
where: {
status: 'LIVE'
},
take: 1,
orderBy: {
createdAt: 'desc'
}
},
}
});
这段代码没有工作,但演示了我正在努力实现的目标。
不幸的是,我认为目前无法直接使用 Prisma 查询来完成您希望的操作。不过,我可以建议两种可能的解决方法:
方法 1: 获取最新的 publicationStatus
以及 publication
并检查节点应用程序中的 status
。
这就是查询的样子:
let publication = await prisma.publication.findFirst({
where: {
id
},
include: {
publicationStatus: {
orderBy: {
createdAt: 'desc'
},
take: 1
}
}
});
// check publication.publicationStatus[0].status and handle appropriately
方法 2: 使用 queryRaw
方法编写原始 SQL 查询。
对于单个出版物,我认为使用方法 1 会更容易。但是,如果您想 return 所有 LIVE 出版物(您在评论),方法 1 的性能特征可能不理想。