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

[Question] Query logging/debug produces sql output inconsistent with the server executed query #710

Open
eymerich opened this issue Sep 1, 2023 · 0 comments
Assignees

Comments

@eymerich
Copy link

eymerich commented Sep 1, 2023

My environment is gorm + postgresql + italy GMT+2 (dst).

While messing around with queries with time.Time I had to debug a query where I pass the same time.Time type parameter to use that in a where against two fields of a record of type timestamp and of type timestamptz ex:

-- Example table
create table tmp
(
    t   timestamp without time zone,
    ttz timestamp with time zone
);

golang query

db.Debug().Raw(`select * from tmp where ttz = ? or ttz = ?`, time.Now(), time.Now()).Take(&row)

At this point the gorm logger produces this output:

[0.235ms] [rows:0] select * from tmp where ttz = '2023-09-01 17:14:48.271' or ttz = '2023-09-01 17:14:48.271'

while the Postgres query logger (SELECT set_config('log_statement', 'all', false))

LOG:  execute stmtcache_5: select * from tmp where ttz = $1 or ttz = $2
DETAIL:  parameters: $1 = '2023-09-01 15:14:48.271574+00', $2 = '2023-09-01 15:14:48.271574+00'

The difference here is that gorm logger returns a statement where time has not the timezone, postgres logger instead show that the query has been executed with correct data; if I execute the gorm logged query as is from gorm logger, since it's missing the timezone indication/correction (as the one of postgres) I'll not get the same result that instead the server is correctly returning.

Examining gorm codebase I see that in file gorm/logger/sql.go there is the date format

const (
	tmFmtWithMS = "2006-01-02 15:04:05.999"
	tmFmtZero   = "0000-00-00 00:00:00"
	nullStr     = "NULL"
)

that will not show timezone while producing debugged sql output, is that intentional and I have to understand that such logging is producing a query that actually will be different than the server generated one (hence possibily producing different output than expected) or could be that a bug?

thx for reading

@eymerich eymerich changed the title [Question] Query logging/debug produced sql output inconsistent with the server executed query [Question] Query logging/debug produces sql output inconsistent with the server executed query Sep 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants