首页 > 解决方案 > 如何在两个表之间建立关系并进行有效查询?

问题描述

这是我的数据库架构:

model Logistics {
  logisticsid     String   @id
  logistics_store Int
  logistics_date  DateTime
  Stores          Stores?  @relation(fields: [logistics_store], references: [storeid])
}

model Stores {
  storeid   Int         @id
  ka        Int
  country   String
  name      String
  city      String
  address   String
  kgrp_name String
  password  String
  cxw       Int
  cx        Int
  plz       Int
  Logistics Logistics[]
}

这是我查询数据库并将结果发送回客户端的 API:

export default async function handle(req, res) {
  await runMiddleware(req, res, cors);
  try {
    const logistics = await prisma.logistics.findMany({
      where: { logistics_store: storeid },
      include: { Stores }
    });
    res.json(logistics);
  } catch (error) {
    console.log(error);
  }
}

我在这里想要实现的是获得Logistics包含 3 个列的完整表格,并获得Storeswhere logistics_store = storeid

标签: next.jsprismaprisma2

解决方案


推荐阅读