Prisma Count Issue: Different Results in Local vs. Production
Unanswered
Dwarf Crocodile posted this in #help-forum
Dwarf CrocodileOP
I'm using Prisma with Neon DB (free tier). I have a waitlist feature where I count the number of signups using prisma.waitlist.count(). Locally, it returns 50 (in DB there are 50 fields), but in production, it returns 36.
This is the route.js for count-api:
This is the route.js for count-api:
import { NextResponse } from "next/server";
import { prisma } from "../../../../utils/prisma";
export async function GET() {
try {
const count = await prisma.waitlist.count();
return NextResponse.json({ count }, { status: 200 });
} catch (error) {
return NextResponse.json(
{ message: "Error fetching count" },
{ status: 500 }
);
}
}
77 Replies
Dwarf CrocodileOP
if this helps:
This is my
This is my
/utils/prisma.js
:import { PrismaClient } from "@prisma/client";
export const prisma = new PrismaClient();
Dutch
are you getting some errors at prod
Dwarf CrocodileOP
no nothing
Dutch
maybe at console or terminal
Dwarf CrocodileOP
got this response from API:
{"count":36}
{"count":36}
i have same DB 🙂
is it some caching issue or something?
Dutch
can you share your prod link
@Dutch can you share your prod link
Dwarf CrocodileOP
can i DM?
was just in middle of something.
or wait..
can i share a " Preview Deployment" vercel link?
Dutch
yep, whatever have problem
where should i look
Dwarf CrocodileOP
in herosection
there's a count
of total signups
in DB there are more than 50 rows....
I am gettign only 36 from the API
Dutch
it shows 136 😄
@Dutch it shows 136 😄
Dwarf CrocodileOP
I am starting from 100 😊😁
@Dutch it shows 136 😄
Dwarf CrocodileOP
...
@Dwarf Crocodile I am starting from 100 😊😁
Dutch
is it same when you query db directly
maybe there are some invalid rows
Dwarf CrocodileOP
locally....i get the API response as count: 56
@Dutch maybe there are some invalid rows
Dwarf CrocodileOP
what..how?
i am doing exaclty same...for both prod n local
according to you, what qualifies as an invalid row?
Dutch
a row with a empty or not valid input
id: 1 | "1"
Dwarf CrocodileOP
// waitlist schema
model Waitlist {
id String @id @default(cuid())
email String @unique
createdAt DateTime @default(now())
}
model Waitlist {
id String @id @default(cuid())
email String @unique
createdAt DateTime @default(now())
}
only 3 fields....any of them can't be null or empty
Dutch
but its not saying cant be null
Dwarf CrocodileOP
i mean..i am just sending "email" to database when a user fills the form, and 'id' and 'createdAt' are filled automatically...
how can they be invalid
and if they are invalid...why is it working locally
Dutch
can you fill database directly by your hand and deploy again
at your scheme there is no field says a field cant be null
all are nullable and dont have default when its empty
@Dutch at your scheme there is no field says a field cant be null
Dwarf CrocodileOP
this is true..
but...in DB, i dont see any empty fields
all are with correct time stamps
same format...
am i making sense?
Dutch
okay pls try to fill db manually with 1 record and try again
@Dwarf Crocodile and if they are invalid...why is it working locally
Dwarf CrocodileOP
also...
@Dutch all are nullable and dont have default when its empty
Dwarf CrocodileOP
sorry sir....but...
for
model Waitlist {
id String @id @default(cuid())
email String @unique
createdAt DateTime @default(now())
}
for
id
and createdAt
i am providing a defualt value....So these fields are not nullable and will always have a value.
@Anay-208 | Ping in replies sorry for the ping...
In prod, what happens if we try to get all items and console log?
Dutch
id string unique not null
@Anay-208 | Ping in replies In prod, what happens if we try to get all items and console log?
Dwarf CrocodileOP
srry...was about to try this..i should have tested it before posting here..mb
dw, Let me know when you try
@Dwarf Crocodile sorry sir....but...
model Waitlist {
id String @id @default(cuid())
email String @unique
createdAt DateTime @default(now())
}
for `id` and `createdAt` i am providing a defualt value....
Dwarf CrocodileOP
just want to know if I am correct here or not
Dutch
i just wanted one thing from you and
@Anay-208 | Ping in replies dw, Let me know when you try
Dwarf CrocodileOP
found one thing...After I deploy, the production site shows the correct waitlist count, but any new signups don't reflect until I redeploy again to prod.
@Dwarf Crocodile is it some caching issue or something?
Dwarf CrocodileOP
.?
whats your nextjs version
Dwarf CrocodileOP
14
🙂
"next": "14.2.11",
@Anay-208 | Ping in replies it could be due to caching
Dwarf CrocodileOP
is this of any help here:
export const dynamic = 'force-dynamic';
Dwarf CrocodileOP
just mention it on top of my route?
@Anay-208 | Ping in replies in 14 its cached by default
Dwarf CrocodileOP
oh
@Dwarf Crocodile just mention it on top of my route?
just add it before your function
@Anay-208 | Ping in replies just add it before your function
Dwarf CrocodileOP
Solved! thanks
wanted to clear 2 more things...
Can i nest APIs like this to organise? (
Can i nest APIs like this to organise? (
/waitlist
is called to store email to DB, and /count
is called to show total counts)Dwarf CrocodileOP
yes
cool 👍
@Anay-208 | Ping in replies yes, so the route created will be `/waitlist/count`
Dwarf CrocodileOP
and secondly..
@Dwarf Crocodile just want to know if I am correct here or not
Dwarf CrocodileOP
this..