0

I am using Prisma to handle database queries.

Currently the database does not return the items array whether it'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'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: "NOT_FOUND" });

      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"
            ]
          }
        }
      }
    }
  }
]
ℛɑƒæĿᴿᴹᴿ
  • 4,983
  • 4
  • 38
  • 58
bysiuxvx
  • 65
  • 2
  • 10

1 Answers1

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:

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