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]: Postgres insert into timestamp with time zone removes milliseconds #1061

Closed
hapawillow opened this issue Aug 15, 2023 · 13 comments
Closed
Assignees
Labels
bug Something isn't working

Comments

@hapawillow
Copy link

What version of drizzle-orm are you using?

0.27.2

What version of drizzle-kit are you using?

0.19.12

Describe the Bug

When I insert a timestamp the milliseconds are removed.

  • Column definition: lastSignInAt: timestamp('last_sign_in_at', { withTimezone: true }).notNull()
  • Data I'm inserting: const now = new Date();
  • console.log(now) displays 2023-08-15T21:36:45.531Z
  • Sample insert statement: const data = await db.insert(accounts).values({ lastSignInAt: now }).returning({ createdAt: accounts.createdAt });
  • console.log(data.createdAt) displays 2023-08-15T21:36:45.000Z

Might be related to issued #877

Expected behavior

Timestamp should be stored with milliseconds
In the example above, data.created should be 2023-08-15T21:36:45.531Z

Environment & setup

No response

@nicobao
Copy link

nicobao commented Nov 23, 2023

For me it inserts the milliseconds, but then selects by truncating them. So when I do a comparison in a where clause using data previously selected, I get unexpected results :/.
I use timestamp without timezone.

@Angelelz
Copy link
Collaborator

This will be closed by #1659

@Angelelz
Copy link
Collaborator

You'll just need to use the mode: 'string' because the javascript Date object doen't support microsecond precision.

@nicobao
Copy link

nicobao commented Jan 6, 2024

You'll just need to use the mode: 'string' because the javascript Date object doen't support microsecond precision.

Javascript Date supports milliseconds:

> new Date()
2024-01-06T20:52:05.354Z
> new Date().getMilliseconds()
862

You said microseconds. I don't care about those but I'd like to keep the milliseconds.

Can I do this to keep the "date" mode:

timestamp("updated_at", {
        mode: "date",
        precision: 3, // 3 for milliseconds
  })

According to the Postgres doc, Postgres default to the precision of the literal value, so it should have been 3 by default, no?

@WildEgo
Copy link

WildEgo commented Jan 10, 2024

You'll just need to use the mode: 'string' because the javascript Date object doen't support microsecond precision.

Javascript Date supports milliseconds:

> new Date()
2024-01-06T20:52:05.354Z
> new Date().getMilliseconds()
862

You said microseconds. I don't care about those but I'd like to keep the milliseconds.

Can I do this to keep the "date" mode:

timestamp("updated_at", {
        mode: "date",
        precision: 3, // 3 for milliseconds
  })

According to the Postgres doc, Postgres default to the precision of the literal value, so it should have been 3 by default, no?

Currently facing the same issue on a returning query, when I Select it normally it seems to work perfectly but for some reason the returning just removes the precision

@Angelelz
Copy link
Collaborator

You'll just need to use the mode: 'string' because the javascript Date object doen't support microsecond precision.

Javascript Date supports milliseconds:

> new Date()
2024-01-06T20:52:05.354Z
> new Date().getMilliseconds()
862

You said microseconds. I don't care about those but I'd like to keep the milliseconds.
Can I do this to keep the "date" mode:

timestamp("updated_at", {
        mode: "date",
        precision: 3, // 3 for milliseconds
  })

According to the Postgres doc, Postgres default to the precision of the literal value, so it should have been 3 by default, no?

Currently facing the same issue on a returning query, when I Select it normally it seems to work perfectly but for some reason the returning just removes the precision

They just published a fix for this in the beta tag. Can you check if that fixes the issue?

@WildEgo
Copy link

WildEgo commented Jan 10, 2024

You'll just need to use the mode: 'string' because the javascript Date object doen't support microsecond precision.

Javascript Date supports milliseconds:

> new Date()
2024-01-06T20:52:05.354Z
> new Date().getMilliseconds()
862

You said microseconds. I don't care about those but I'd like to keep the milliseconds.
Can I do this to keep the "date" mode:

timestamp("updated_at", {
        mode: "date",
        precision: 3, // 3 for milliseconds
  })

According to the Postgres doc, Postgres default to the precision of the literal value, so it should have been 3 by default, no?

Currently facing the same issue on a returning query, when I Select it normally it seems to work perfectly but for some reason the returning just removes the precision

They just published a fix for this in the beta tag. Can you check if that fixes the issue?

I'm currently at the gym, I beg give it a look in a couple of hours, just let me know the version and I'll upgrade

@WildEgo
Copy link

WildEgo commented Jan 11, 2024

@Angelelz Hey mate just to let u know drizzle-orm@0.29.3-8c0788a corrects the issue indeed

nadeesha added a commit to differentialhq/differential that referenced this issue Jan 31, 2024
Due to drizzle-team/drizzle-orm#1061 the
events lose the ms precision on the timestamp. This PR converts the
query to a raw query, so the precision is preserved.
@mdurling
Copy link

mdurling commented Feb 28, 2024

This is still a problem for inserts that use .returning() ... It's not a problem for selects. Also, I only experience the problem with the postgres driver. neon-serverless works correctly OTOH.

    "@neondatabase/serverless": "0.8.1",
    "drizzle-orm": "0.29.4",
    "postgres": "3.4.3",

@AndriiSherman
Copy link
Member

Should be fixed in drizzle-orm@beta. I would appreciate some feedback to confirm whether this issue has been resolved in this tag.

I plan to release it in version 0.30.0 tomorrow or within the next few days; I simply aim to address this substantial set of issues we're encountering. I'll be duplicating this message across all similar issues we're facing.

@WildEgo
Copy link

WildEgo commented Mar 6, 2024

Should be fixed in drizzle-orm@beta. I would appreciate some feedback to confirm whether this issue has been resolved in this tag.

I plan to release it in version 0.30.0 tomorrow or within the next few days; I simply aim to address this substantial set of issues we're encountering. I'll be duplicating this message across all similar issues we're facing.

I've previously replied to this and stated that it was working perfectly for me and it not being on stable was breaking my environment

@mdurling
Copy link

mdurling commented Mar 7, 2024

The issue I reported with .returning() is resolved in drizzle-orm 0.29.5-e0ead65.

@AndriiSherman
Copy link
Member

Fixed in drizzle-orm@0.30.0

Please check release notes for more info

@github-project-automation github-project-automation bot moved this from Todo to Done in Public Roadmap Mar 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
Status: Done
Development

No branches or pull requests

7 participants