NextJS开发:Prisma数据库多表关联查询,使用include替代left join

Prisma中的多表关联查询实例

1、schema.prisma中定义模型

typescript 复制代码
model Account {
  @@map("account")
  accountId         Int @id @default(autoincrement()) @map("account_id")
  nickName          String? @db.VarChar(32) @map("nick_name")
  pwd               String? @db.VarChar(128)
  mobile            String? @db.VarChar(11)
  email             String? @db.VarChar(20)
  createAt          DateTime @map("create_at")
  lastLoginAt       DateTime? @map("last_login_at")
  chapters          Chapter[]
}

model Chapter {
  @@map("d_chapter")
  chapterId         BigInt @id @default(autoincrement()) @map("chapter_id")
  account           Account @relation(fields: [accountId], references: [accountId])
  accountId         Int @map("account_id")
  ...
  createAt          DateTime @map("create_at")
  updateAt          DateTime? @map("update_at")
  content           ChapterContent?
}

2、查询操作

typescript 复制代码
let chapters = await Db.share().chapter.findMany({
  orderBy: {
    chapterId: "desc"
  },
  skip: offset,
  take: limit,
  include: {
    account: true
  }
})

3、查看日志中生成sql

复制代码
prisma:query SELECT `nextbbsdb`.`d_chapter`.`chapter_id`, `nextbbsdb`.`d_chapter`.`account_id`, `nextbbsdb`.`d_chapter`.`chapter_type`, `nextbbsdb`.`d_chapter`.`doc_id`, `nextbbsdb`.`d_chapter`.`topic_id`, `nextbbsdb`.`d_chapter`.`pid`, `nextbbsdb`.`d_chapter`.`state`, `nextbbsdb`.`d_chapter`.`type`, `nextbbsdb`.`d_chapter`.`chapter_name`, `nextbbsdb`.`d_chapter`.`thumb`, `nextbbsdb`.`d_chapter`.`file_format`, `nextbbsdb`.`d_chapter`.`keywords`, `nextbbsdb`.`d_chapter`.`intro`, `nextbbsdb`.`d_chapter`.`browse_num`, `nextbbsdb`.`d_chapter`.`reply_num`, `nextbbsdb`.`d_chapter`.`order_no`, `nextbbsdb`.`d_chapter`.`create_at`, `nextbbsdb`.`d_chapter`.`update_at`, `nextbbsdb`.`d_chapter`.`audit_at`, `nextbbsdb`.`d_chapter`.`is_del` FROM `nextbbsdb`.`d_chapter` WHERE `nextbbsdb`.`d_chapter`.`state` = ? ORDER BY `nextbbsdb`.`d_chapter`.`chapter_id` DESC LIMIT ? OFFSET ?
prisma:query SELECT `nextbbsdb`.`account`.`account_id`, `nextbbsdb`.`account`.`type`, `nextbbsdb`.`account`.`nick_name`, `nextbbsdb`.`account`.`pwd`, `nextbbsdb`.`account`.`mobile`, `nextbbsdb`.`account`.`email`, `nextbbsdb`.`account`.`create_at`, `nextbbsdb`.`account`.`last_login_at` FROM `nextbbsdb`.`account` WHERE `nextbbsdb`.`account`.`account_id` IN (?)

通过sql日志可以看出,Prisma是通过两条sql完成的查询,先查出chapter列表,在使用chapter中的主键集合,使用In查询的account,之后再组合数据进行结果返回。

4、如果想要直接使用left join查询,只能使用prisma提供的原始数据库访问方法了

复制代码
const rows = await Db.share().$queryRaw`SELECT a.*,b.nick_name FROM d_chapter a left join account b on a.account_id=b.account_id`
相关推荐
浪遏5 小时前
recruitment-cli:让 AI 接手求职招聘,从架构到一次发送超时
架构·typescript·node.js
律宏阔17 小时前
微信小程序 + Taro 从 ESLint 迁移到 Oxlint 让项目代码检查速度提升近 12 倍
前端·typescript·eslint
瓦瓦gogo18 小时前
从一个 TypeScript 函数开始理解 MCP 协议
linux·ubuntu·typescript
ZhengEnCi21 小时前
React 19 加 Vite 6 企业级前端项目目录结构:无路由 SPA、zustand 与 Tailwind 4 的分层架构指南
react.js
用户2986985301421 小时前
React 前端实现 Excel 与 CSV 互转:基于 WebAssembly 的浏览器端方案
javascript·react.js·excel
ZhengEnCi1 天前
Hono 4 多库多模型服务的企业级 Monorepo 目录结构:npm workspaces 与 Turborepo 该怎么分层
typescript
沉默小管2 天前
我用 NestJS + Nuxt 3 做了个导航站,聊几个踩过的坑
node.js·seo
浪遏2 天前
从命令到工作流:juejin-cli 如何让写作自动化更可靠
typescript
非优秀程序员2 天前
第02章 杭电的英语老师与一台服务器|《互联网产品恩仇录》
数据库·node.js·代码规范
Hone3 天前
juejin-skills 系列 ②:我把「看掘金热榜」也做成了 Agent 技能
开源·node.js·ai编程