我使用 Prisma 作为 ORM 从表中获取数据,包括其关系。这是我的 findMany 调用的参数:
{
take: 50,
skip: 0,
include: {
AprRecords: {
take: 1,
orderBy: [Object ...],
include: [Object ...],
},
TvlRecords: {
take: 1,
orderBy: [Object ...],
include: [Object ...],
},
DailyRewardsRecords: {
take: 1,
orderBy: [Object ...],
include: [Object ...],
},
MainProtocol: true,
Chain: true,
Protocols: true,
Tokens: true,
},
orderBy: {
dailyRewards: "desc",
},
where: {
OR: undefined,
},
}
以下是生成的 SQL 请求:
prisma:query SELECT "public"."Opportunity"."id", "public"."Opportunity"."chainId", "public"."Opportunity"."type"::text, "public"."Opportunity"."identifier", "public"."Opportunity"."name", "public"."Opportunity"."status"::text, "public"."Opportunity"."action"::text, "public"."Opportunity"."mainProtocolId", "public"."Opportunity"."tvl", "public"."Opportunity"."apr", "public"."Opportunity"."dailyRewards", "public"."Opportunity"."tags" FROM "public"."Opportunity" WHERE 1=1 ORDER BY "public"."Opportunity"."dailyRewards" DESC LIMIT $1 OFFSET $2
prisma:query SELECT "public"."AprRecord"."id", "public"."AprRecord"."timestamp", "public"."AprRecord"."cumulated", "public"."AprRecord"."opportunityId" FROM "public"."AprRecord" WHERE "public"."AprRecord"."opportunityId" IN ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14,$15,$16,$17,$18,$19,$20,$21,$22,$23,$24,$25,$26,$27,$28,$29,$30,$31,$32,$33,$34,$35,$36,$37,$38,$39,$40,$41,$42,$43,$44,$45,$46,$47,$48,$49,$50) ORDER BY "public"."AprRecord"."timestamp" DESC OFFSET $51
prisma:query SELECT "public"."AprBreakdown"."id", "public"."AprBreakdown"."identifier", "public"."AprBreakdown"."type"::text, "public"."AprBreakdown"."value", "public"."AprBreakdown"."aprRecordId" FROM "public"."AprBreakdown" WHERE "public"."AprBreakdown"."aprRecordId" IN ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14,$15,$16,$17,$18,$19,$20,$21,$22,$23,$24,$25,$26,$27,$28,$29,$30,$31,$32,$33,$34,$35,$36,$37,$38,$39,$40,$41,$42,$43,$44,$45,$46,$47,$48,$49,$50) OFFSET $51
prisma:query SELECT "public"."TVLRecord"."id", "public"."TVLRecord"."timestamp", "public"."TVLRecord"."total", "public"."TVLRecord"."opportunityId" FROM "public"."TVLRecord" WHERE "public"."TVLRecord"."opportunityId" IN ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14,$15,$16,$17,$18,$19,$20,$21,$22,$23,$24,$25,$26,$27,$28,$29,$30,$31,$32,$33,$34,$35,$36,$37,$38,$39,$40,$41,$42,$43,$44,$45,$46,$47,$48,$49,$50) ORDER BY "public"."TVLRecord"."timestamp" DESC OFFSET $51
prisma:query SELECT "public"."TVLBreakdown"."id", "public"."TVLBreakdown"."identifier", "public"."TVLBreakdown"."type"::text, "public"."TVLBreakdown"."value", "public"."TVLBreakdown"."tvlRecordId" FROM "public"."TVLBreakdown" WHERE "public"."TVLBreakdown"."tvlRecordId" IN ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14,$15,$16,$17,$18,$19,$20,$21,$22,$23,$24,$25,$26,$27,$28,$29,$30,$31,$32,$33,$34,$35,$36,$37,$38,$39,$40,$41,$42,$43,$44,$45,$46,$47,$48,$49,$50) OFFSET $51
prisma:query SELECT "public"."DailyRewardsRecord"."id", "public"."DailyRewardsRecord"."timestamp", "public"."DailyRewardsRecord"."total", "public"."DailyRewardsRecord"."opportunityId" FROM "public"."DailyRewardsRecord" WHERE "public"."DailyRewardsRecord"."opportunityId" IN ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14,$15,$16,$17,$18,$19,$20,$21,$22,$23,$24,$25,$26,$27,$28,$29,$30,$31,$32,$33,$34,$35,$36,$37,$38,$39,$40,$41,$42,$43,$44,$45,$46,$47,$48,$49,$50) ORDER BY "public"."DailyRewardsRecord"."timestamp" DESC OFFSET $51
prisma:query SELECT "public"."DailyRewardsBreakdown"."id", "public"."DailyRewardsBreakdown"."value", "public"."DailyRewardsBreakdown"."campaignId", "public"."DailyRewardsBreakdown"."dailyRewardsRecordId" FROM "public"."DailyRewardsBreakdown" WHERE "public"."DailyRewardsBreakdown"."dailyRewardsRecordId" IN ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14,$15,$16,$17,$18,$19,$20,$21,$22,$23,$24,$25,$26,$27,$28,$29,$30,$31,$32,$33,$34,$35,$36,$37,$38,$39,$40,$41,$42,$43,$44,$45,$46,$47,$48,$49,$50) OFFSET $51
prisma:query SELECT "public"."Protocol"."id", "public"."Protocol"."tags"::text[], "public"."Protocol"."name", "public"."Protocol"."description", "public"."Protocol"."url", "public"."Protocol"."icon" FROM "public"."Protocol" WHERE "public"."Protocol"."id" IN ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10) OFFSET $11
prisma:query SELECT "public"."Chain"."id", "public"."Chain"."name", "public"."Chain"."icon" FROM "public"."Chain" WHERE "public"."Chain"."id" IN ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11) OFFSET $12
prisma:query SELECT "public"."_OpportunityToProtocol"."A", "public"."_OpportunityToProtocol"."B" FROM "public"."_OpportunityToProtocol" WHERE "public"."_OpportunityToProtocol"."A" IN ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14,$15,$16,$17,$18,$19,$20,$21,$22,$23,$24,$25,$26,$27,$28,$29,$30,$31,$32,$33,$34,$35,$36,$37,$38,$39,$40,$41,$42,$43,$44,$45,$46,$47,$48,$49,$50)
prisma:query SELECT "public"."_OpportunityToToken"."A", "public"."_OpportunityToToken"."B" FROM "public"."_OpportunityToToken" WHERE "public"."_OpportunityToToken"."A" IN ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14,$15,$16,$17,$18,$19,$20,$21,$22,$23,$24,$25,$26,$27,$28,$29,$30,$31,$32,$33,$34,$35,$36,$37,$38,$39,$40,$41,$42,$43,$44,$45,$46,$47,$48,$49,$50)
prisma:query SELECT "public"."Token"."id", "public"."Token"."name", "public"."Token"."chainId", "public"."Token"."address", "public"."Token"."decimals", "public"."Token"."symbol", "public"."Token"."icon", "public"."Token"."verified", "public"."Token"."price" FROM "public"."Token" WHERE "public"."Token"."id" IN ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14,$15,$16,$17,$18,$19,$20,$21,$22,$23,$24,$25,$26,$27,$28,$29,$30,$31,$32,$33,$34,$35,$36,$37,$38,$39,$40,$41,$42,$43,$44,$45,$46,$47,$48,$49,$50,$51,$52,$53,$54,$55,$56,$57,$58,$59,$60,$61,$62,$63,$64,$65,$66,$67,$68,$69,$70,$71,$72,$73,$74,$75,$76,$77,$78,$79,$80,$81,$82,$83,$84,$85,$86,$87,$88) OFFSET $89
如您所见,包含的关系(AprRecords、TvlRecords、DailyRewardsRecords)的参数中有一个
take: 1
子句。但是,生成的 SQL 请求中没有 LIMIT 子句,导致返回数万行而不是 1。
Prisma 的 findMany() 方法返回正确的结果,但在应用程序级别而不是数据库级别执行
take: 1
。
如何在数据库级别的嵌套读取中仅检索一条记录?
Prisma 团队已知此问题,但尚未解决: https://github.com/prisma/prisma/issues/15623