Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG]: decimal fields serialize to number (instead of string) when added in a with query #3943

Open
1 task done
carderne opened this issue Jan 13, 2025 · 0 comments
Open
1 task done
Labels
bug Something isn't working db/postgres priority Will be worked on next qb/crud

Comments

@carderne
Copy link

carderne commented Jan 13, 2025

Report hasn't been filed before.

  • I have verified that the bug I'm about to report hasn't been filed before.

What version of drizzle-orm are you using?

0.38.2

What version of drizzle-kit are you using?

0.30.1

Other packages

No response

Describe the Bug

Summary

If you have a Postgres column with type decimal(), it is correctly serialised to type string in JavaScript.

However, if you load a referenced table that has a decimal column, it is incorrectly serialised to type number in JavaScript.

Full reproduction

This is the issue:

// query.ts
import { db } from "./db";
import * as schema from "./schema";

const [parent] = await db.insert(schema.parent).values({ parentVal: "12.34" }).returning();
await db.insert(schema.child).values({ parentId: parent?.id, childVal: "56.78" });
const res = await db.query.child.findFirst({ with: { parent: true } });

console.log(typeof res?.childVal);         // === "string" ✅
console.log(typeof res?.parent.parentVal); // === "number" ❌

Setup code used here:

// schema.ts
import { relations } from "drizzle-orm";
import { decimal, pgTable, serial } from "drizzle-orm/pg-core";

export const parent = pgTable("parent", {
  id: serial().primaryKey(),
  parentVal: decimal(),
});
export const child = pgTable("child", {
  parentId: serial().references(() => parent.id),
  childVal: decimal(),
});
export const childRelations = relations(child, ({ one }) => ({
  parent: one(parent, {
    fields: [child.parentId],
    references: [parent.id],
  }),
}));
// db.ts
import { drizzle } from "drizzle-orm/node-postgres";
import pg from "pg";
import * as schema from "./schema";

export const DATABASE_URL = "postgres://...";
const client = new pg.Pool({ connectionString: DATABASE_URL });
export const db = drizzle({ schema, client });
@carderne carderne added the bug Something isn't working label Jan 13, 2025
@L-Mario564 L-Mario564 added db/postgres priority Will be worked on next qb/crud labels Feb 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working db/postgres priority Will be worked on next qb/crud
Projects
None yet
Development

No branches or pull requests

2 participants