drizzle-team / drizzle-orm

Headless TypeScript ORM with a head. Runs on Node, Bun and Deno. Lives on the Edge and yes, it's a JavaScript ORM too 😅
https://orm.drizzle.team
Apache License 2.0
21.57k stars 490 forks source link

[BUG]: value.getTime is not a function #2323

Open hmh6a opened 1 month ago

hmh6a commented 1 month ago

What version of drizzle-orm are you using?

0.30.10

What version of drizzle-kit are you using?

0.21.2

Describe the Bug

Bug Report: Error when using time-related queries

Description

When attempting to use time-related queries with drizzle-kit, I encounter an error. Specifically, the error occurs when trying to fetch records from the visits table where the createdAt timestamp is greater than or equal to the current time.

Code Snippets

Here is the code that produces the error:

const visits = await db.query.visits.findMany({
    where: gte(tables.visits.createdAt, Date.now()),
})

Schema definition for the visits table:

export const visits = sqliteTable('visits', {
    id: integer('id').primaryKey({ autoIncrement: true }),
    // ....
    createdAt: integer('created_at', { mode: 'timestamp' }).notNull().default(new Date().getTime()),
})

Error Message

Error: value.getTime is not a function

Expected behavior

No response

Environment & setup

No response

magicalpuffin commented 1 month ago

I don't have any issues with filtering, for your example, does gte(tables.visits.createdAt, new Date()) work?

I am getting the same error, TypeError: value.getTime is not a function, but only when updating. Here is my database schema.

    createdDate: integer("created_date", { mode: "timestamp" })
      .default(sql`(unixepoch())`)
      .notNull(),
    updatedDate: integer("updated_date", { mode: "timestamp" })
      .$onUpdate(() => sql`(unixepoch())`)
      .notNull(),

I suspect there is some epoch time vs JS date type issue. .$onUpdate(() => new Date()) works and so does setting it when updating with updatedDate: sql(unixepoch())``. It is just odd that it doesn't work in the schema onUpdate function.

hmh6a commented 1 month ago

@magicalpuffin

You should change type from integer into text My problem solved like this

bruceharrison1984 commented 1 month ago

@magicalpuffin I've got the same issue. I was happy to see that they added onUpdate but after refactoring to take advantage of it, I realized it didn't work on timestamps so I'm back to manually pushing updateOn values until it gets fixed.

I also don't want a text column, because it's a timestamp, not text.

Whats-A-MattR commented 5 days ago

Hopefully we see a fix soon, this one's really annoying.

Whats-A-MattR commented 5 days ago

I did find what seems to be a way to do it. As long as you're okay with epoch time.

export const tableName = sqliteTable("tablename", {
    id: ...,
    updated_at: integer("updated_at", { mode: "timestamp_ms" })
        .$onUpdateFn(() => new Date()).$type<Date>(),
    otherFields: ...
}

I've only tested this with SQLite, but it seems to be working for me.