In my deployed Node REST API service, I am seeing an unhelpful error when attempting to connect with with one particular table (and not the others). The error reads "TypeError: Cannot read properties of undefined (reading 'findMany')"
As I said, I can read and write to other tables without a problem. When I run the api locally, I can read and write to the problematic table (UserFavorite). I am connecting to the same instance of the database locally and from my deployed api.
My queries to those two tables are nearly identical. What am I missing here? Schema and queries pasted below.
SCHEMA:
generator client {
provider = "prisma-client-js"
}
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
model User {
id Int @id @default(autoincrement())
createdAt DateTime @default(now()) @map("created_at")
clerkId String @unique @map("clerk_id")
userFavorites UserFavorite[]
@@map("user")
}
model UserFavorite {
id Int @id @default(autoincrement())
createdAt DateTime @default(now()) @map("created_at")
articleId String @map("article_id")
user User @relation(fields: [userId], references: [id])
userId Int @map("user_id")
@@unique([articleId, userId])
@@index([slug, userId(sort: Asc)])
@@map("user_favorite")
}
SERVICE:
const prisma = new PrismaClient();
export async function getUserByClerkId(clerkId: string) {
try {
const user = await prisma.user.findUnique({
where: { clerkId }
});
return user;
} catch (err) {
console.error('Error getting user', err);
throw err;
}
}
export async function getUserFavorites(userId: number) {
try {
const favs = await prisma.userFavorite.findMany({
where: {
userId,
}
});
return favs;
} catch (err) {
console.error('Error getting user favorites', err);
throw err;
}
}
This problem popped up after I renamed the UserFavorite table. At first it seemed to be attempting to connect to the old table name. Since then, I have:
prisma format
on the Prisma schema using the Prisma CLIUPDATE: In retrospect, it seems like this is the error I see when the Prisma migration hasn't actually run, and the expected database tables do not exist. I assume that's what was going on here. I assume that the below solution worked because I had to re-run migrations, which created the tables AND renamed the one (but the renaming was not the solution).
ORIGINAL: Huh. It seems like I should be able to have a PascalCase entity in my schema, renamed to a snake_case table name in my database, and accessed in code as prisma.entityName. I am not sure why that isn't working, but the workaround I found is to just rename the table to favorites
so I don't have to worry about casing.
Oh, I also added a postinstall script to my package.json, so that could have been what fixed it: