WaspW
Wasp3y ago
KYAN1TE

[SOLVED] Object relation works when running app but doesn't compile to TypeScript?

My database schema looks as follows:

entity Task {=psl
    id          Int     @id @default(autoincrement())
    description String
    isDone      Boolean @default(false)
    user        User?    @relation(fields: [userId], references: [id])
    userId      Int?
    category    Category @relation(fields: [categoryId], references: [id])
    categoryId  Int
psl=}

entity Category {=psl
  id   Int    @id @default(autoincrement())
  name String
  tasks Task[] 
psl=}


Meanwhile on my client when I do:
      <span className={task.isDone ? "line-through text-black" : "text-black"}>
        {task.description} ({task.category.name})
      </span>


I get the following TypeScript error under task.category.name:
Property 'category' does not exist on type 'GetResult<{ id: number; description: string; isDone: boolean; userId: number | null; categoryId: number; }, unknown> & {}'. Did you mean 'categoryId'?ts(2551)

I have seen examples within the Wasp repo that allow this to work.
This is done by using the includes part of the ORM like so:
import { Task } from "@wasp/entities";
import { GetAllUserTasksQuery } from "@wasp/queries/types";
import HttpError from "@wasp/core/HttpError.js";

export const getAllUserTasksQuery: GetAllUserTasksQuery<void, Task[]> = async (
  args,
  context
) => {
  if (!context.user) {
    throw new HttpError(401);
  }

  return context.entities.Task.findMany({
    where: { user: { id: context.user.id } },
    include: { category: true },
    orderBy: { id: "asc" },
  });
};


However despite this, it still seems like I am missing a trick to get the type safety to understand that a Task is expected to reference Category (object) as well as the CategoryId?

This is also blocking me from successfully deploying to Fly.io

Thanks
Was this page helpful?