“Prisma查询未返回模式中指定的数组。”

huangapple go评论99阅读模式
英文:

Prisma query does not return array specified in schema

问题

I understand, I'll provide the translations for the code parts:

  1. 我正在使用 Prisma 处理数据库查询。
  2. 当前数据库无论是空的还是包含条目,都不返回 items 数组。
  3. 这个模式有效,我可以执行 CRUD 操作。
  4. Prisma Studio Supabase 中,ShoppingList items 数组中可见 ShoppingListItems,但在请求中没有返回该数组。
  5. 我在 ShoppingList 中包含了一个 testArray,其中我不执行任何查询,只是为了检查它是否会显示出来,而它确实显示出来。
  6. 已执行步骤:
  7. + 运行 ```pnpm prisma generate```
  8. + 推送到 supabase ```pnpm prisma db push```
  9. 为什么我无法获取 items 数组,无论是空的还是包含条目?我漏掉了什么?
  10. Prisma 模式:
  11. ```plaintext
  12. // 这部分保持不变

ShoppingList 的获取器(按 id):

  1. // 这部分保持不变

ShoppingList 和 ShoppingListItem 的 JSON 响应:

  1. // 这部分保持不变
  1. <details>
  2. <summary>英文:</summary>
  3. I am using Prisma to handle database queries.
  4. Currently the database does not return the items array whether it&#39;s empty or has entries.
  5. This schema works, I am able to perform CRUD actions.
  6. `ShoppingListItems` are visible in the items array of the `ShoppingList` in Prisma studio and on Supabase, but the array is not being returned on requests.
  7. I have included a testArray in the `ShoppingList` where I don&#39;t perform any queries, just to check whether it will show up, and it does.
  8. Steps taken:
  9. + ran ```pnpm prisma generate```
  10. + pushed to supabase ```pnpm prisma db push```
  11. Why am I not getting the items array whether empty or containing entries? What am I missing?
  12. Prisma schema:

datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}

model ShoppingListItem {
id String @id @default(cuid())
name String
shoppingList ShoppingList @relation(fields: [shoppingListId], references: [id])
shoppingListId String
markedDone Boolean @db.Boolean @default(false)
addedById String
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt

@@index([addedById, shoppingListId])
}

model ShoppingList {
id String @id @default(cuid())
name String
items ShoppingListItem[]
testArray String[]
createdById String
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
HouseHold HouseHold? @relation(fields: [houseHoldId], references: [id])
houseHoldId String?

@@index([id])
}

  1. ShoppingList getter (by id):
  2. --

getById: publicProcedure
.input(
z.object({
id: z.string(),
})
)
.query(async ({ input }) => {
const list = await prisma.shoppingList.findUnique({
where: {
id: input.id,
},
});

  1. if (!list) throw new TRPCError({ code: &quot;NOT_FOUND&quot; });
  2. return list;
  3. }),
  1. json response for the ShoppingList and a ShoppingListItem:
  2. -

[
{
"result": {
"data": {
"json": {
"id": "clhnzfqy80000vv27ahbbk0xz",
"name": "groceries",
"testArray": [],
"createdById": "user_2PFljk6joD9dEh3O2AkbDvrIOsC",
"createdAt": "2023-05-14T22:23:09.104Z",
"updatedAt": "2023-05-14T22:23:09.104Z",
"houseHoldId": null
},
"meta": {
"values": {
"createdAt": [
"Date"
],
"updatedAt": [
"Date"
]
}
}
}
}
},
{
"result": {
"data": {
"json": {
"id": "clhnzjb430003vv2795lsl7wj",
"name": "test item",
"shoppingListId": "clhnzfqy80000vv27ahbbk0xz",
"markedDone": false,
"addedById": "user_2PFljk6joD9dEh3O2AkbDvrIOsC",
"createdAt": "2023-05-14T22:25:55.202Z",
"updatedAt": "2023-05-14T22:25:55.202Z"
},
"meta": {
"values": {
"createdAt": [
"Date"
],
"updatedAt": [
"Date"
]
}
}
}
}
}
]

  1. </details>
  2. # 答案1
  3. **得分**: 0
  4. The reason the query was not returning the items array because the items array was not specified to be included in the shoppingList query:
  5. ```js
  6. const list = await prisma.shoppingList.findUnique({
  7. where: {
  8. id: input.id,
  9. },
  10. include:{
  11. items: true
  12. }
  13. });
英文:

The reason the query was not returning the items array because the items array was not specified to be included in the shoppingList query:

  1. const list = await prisma.shoppingList.findUnique({
  2. where: {
  3. id: input.id,
  4. },
  5. include:{
  6. items: true
  7. }
  8. });

huangapple
  • 本文由 发表于 2023年5月15日 07:00:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/76250016.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定