go-gorm/gorm.io

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

eymerich opened this issue · 0 comments

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