drizzle-team / drizzle-team/drizzle-orm

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

Open
#3,943 1 comment 2 reactions 0 assignees View on GitHub
bug bug/fixed-in-beta db/postgres priority qb/crud
Dominant language
TypeScript
Stars
35.8k
Forks
1.6k
Avg merge
2d 7h
Merged PRs (30d)
4

Description

### Report hasn't been filed before.

- [X] 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:
```typescript
// 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:
```typescript
// 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],
}),
}));
```

```typescript
// 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 });
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.