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

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

Prisma query does not return array specified in schema

问题

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

我正在使用 Prisma 处理数据库查询。

当前数据库无论是空的还是包含条目,都不返回 items 数组。

这个模式有效,我可以执行 CRUD 操作。

在 Prisma Studio 和 Supabase 中,ShoppingList 的 items 数组中可见 ShoppingListItems,但在请求中没有返回该数组。

我在 ShoppingList 中包含了一个 testArray,其中我不执行任何查询,只是为了检查它是否会显示出来,而它确实显示出来。

已执行步骤:

+ 运行 ```pnpm prisma generate```
+ 推送到 supabase ```pnpm prisma db push```

为什么我无法获取 items 数组,无论是空的还是包含条目?我漏掉了什么?

Prisma 模式:

```plaintext
// 这部分保持不变

ShoppingList 的获取器(按 id):

// 这部分保持不变

ShoppingList 和 ShoppingListItem 的 JSON 响应:

// 这部分保持不变

<details>
<summary>英文:</summary>

I am using Prisma to handle database queries.

Currently the database does not return the items array whether it&#39;s empty or has entries.

This schema works, I am able to perform CRUD actions.

`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.

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.

Steps taken:

+ ran ```pnpm prisma generate```
+ pushed to supabase ```pnpm prisma db push```

Why am I not getting the items array whether empty or containing entries? What am I missing?

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])
}

ShoppingList getter (by id):
--

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

  if (!list) throw new TRPCError({ code: &quot;NOT_FOUND&quot; });

  return list;
}),

json response for the ShoppingList and a ShoppingListItem:
-

[
{
"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"
]
}
}
}
}
}
]


</details>


# 答案1
**得分**: 0

The reason the query was not returning the items array because the items array was not specified to be included in the shoppingList query:
```js
const list = await prisma.shoppingList.findUnique({
  where: {
    id: input.id,
  },
  include:{
    items: true
  }
});
英文:

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

const list = await prisma.shoppingList.findUnique({
  where: {
    id: input.id,
  },
  include:{
    items: true
  }
});

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:

确定