denodrivers / postgres

PostgreSQL driver for Deno

Home Page:https://denodrivers.github.io/postgres

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Error: value not valid for token {month undefined}

dojyorin opened this issue · comments

An exception was thrown when trying to get a record for table that contained a timestamp type.

// "ts_key" is timestamp type.
await postgres.queryObject("select ts_key from foo_table where id = 1");

// "ts_key" is included.
await postgres.queryObject("select * from foo_table where id = 1");
error: Uncaught (in promise) Error: value not valid for token { month undefined } MAR-22 18:59:59

Converting timestamp type to text type works fine.

await postgres.queryObject("select ts_key::text from foo_table where id = 1");

It is easy if the item you want is one timestamp type, but it is very troublesome if multiple types are mixed by specifying "*".
Since "*::text" is not possible by specification, all keys must be enumerated and only timestamp type keys must be converted with ":: text".

await postgres.queryObject("select name, age, birthday::text, height, registed::text, weight, ... from foo_table");

Is it a specification that this timestamp type cannot be obtained directly?
Also, is there a workaround?

Thank you.

Timestamp can be retrieved when it's in the ISO 8601 format (eg: 1997-12-17 07:37:16-08)

Perhaps your database date format returns a format that I can't parse with date?

Thank you, I solved it!

The cause was that the "DateStyle" in the database was "Redwood" and "SHOW_TIME".

const pg = new Client();
await pg.connect();
await pg.queryObject("set datestyle to 'ISO', 'YMD'");

// Application Body ...

I doing this when connecting to the database, it works perfectly.

You could solve this more efficiently by using the options parameter when initializing the client, there you can set datestyle on connection without having to run that query every time you wanna do something with the database