Skip to content

[BUG]: Numeric & Decimal types expecting a string. #4070

@MGSimard

Description

@MGSimard

After some research it turns out this is supposedly intended by node/pg, it casts the numbers as strings for precision.

Would be nice to include this information in the docs, as has apparently been requested numerous times for months now.
IE: #1042 - Closure without acknowledgement of suggestion, which is tripping up a lot of people.

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.39.1

What version of drizzle-kit are you using?

0.30.4

Other packages

No response

Describe the Bug

To fill this field, please answer the following:

Undesired behaviour:

  • Throwing a type error when attempting to insert numbers into a numeric() or decimal() column.

What are the steps to reproduce it?

  1. Schema:
export const watchedCoins = pgTable("watched_coins", {
  id: serial("id").primaryKey(),
  coinId: integer("coin_id").notNull(),
  startValue: decimal("start_value", { precision: 100, scale: 30 }).notNull(),
  thresholdValue: decimal("threshold_value", { precision: 100, scale: 30 }).notNull(),
  createdAt: timestamp("created_at")
    .default(sql`CURRENT_TIMESTAMP`)
    .notNull(),
  updatedAt: timestamp("updated_at").$onUpdate(() => new Date()),
});
  1. Insert:
    await db.insert(watchedCoins).values({ coinId: 5, startValue: 5, thresholdValue: 5 });

  2. The operation will run fine, but you've got an annoying cryptic type error (which doesn't even underline the right part):
    Image

  3. When verifying with the following, hovering over startValue & thresholdValue will state that your numeric or decimal column expects a string.

const test: typeof watchedCoins.$inferInsert = {
  coinId: 5,
  startValue: 10,
  thresholdValue: 15,
};

Image

What is the desired result?
Don't expect a string out of numeric & decimal alias. It takes in a number.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions