Extremely weird behaviour
Apologies for the lack of better title.
I have come across a weird behaviour where select().from(tablename) returns old and stale data.
import { db } from "@/db/drizzle";
updateCompany({ name: "NAME 1", companyId: 50 })
updateCompany({ name: "NAME 2", companyId: 51 })
updateCompany({ name: "NAME 3", companyId: 52 })
const companies = await db.select().from(companiesTable);
console.log("companies are:", companies);
updateCompany code:
import { db } from "@/db/drizzle";
export async function updateCompany(data: any) {
const companyData = {
...data,
updatedAt: new Date(),
};
const company = await db
.update(companiesTable)
.set(companyData)
.where(eq(companiesTable.companyId, data.companyId))
.returning();
return company[0];
}
You'd expect that returned companies to be NAME 1,..., NAME 3 but I'm getting the old values no matter what I do. The new values are reflected in the database when I check through drizzle-studio and I check NEON db table which means my updateCompany is working but the returned value is incorrect.
I ran out of ideas to troubleshoot. What could I be doing wrong? And what can I do to troubleshoot this further?7 Replies
You are not awaiting updateCompany()
Yep I just noticed and I did that but it doesn't solve the issue
How does your code look now with the awaits?
await updateCompany({ name: "TATATAT 1", companyId: 50 })
await updateCompany({ name: "TATATAT 2", companyId: 51 })
await updateCompany({ name: "TATATAT 3", companyId: 52 })
const companies = await db.select().from(companiesTable);
console.log("companies are:", companies);
The console outputs oldest dataHuh, what if you do it all in a transaction?
No that's not the issue. If I run updateCompany the first time and then comment it and run companies, it still output old data while the db has the new values in
It's extremely weird
Sounds like cache tbh