Skip to content

Logger SQL Output Doesn't Match Actual SQL Due to Missing Timezone Conversion #7628

@huangchong94

Description

@huangchong94

Bug Report: Logger SQL Output Doesn't Match Actual SQL Due to Missing Timezone Conversion

Description

When logging SQL queries, GORM's logger doesn't apply timezone conversion to time parameters, while the underlying MySQL driver does in writeExecutePacket. This causes a mismatch between the logged SQL and the actual SQL executed against the database.

Root Cause

The logger.ExplainSQL() function formats time values without considering the timezone location (loc) configured in the database DSN. However, the MySQL driver converts time values to the configured timezone before sending them to the database.

For example, when DSN contains parseTime=True&loc=Asia/Shanghai, the MySQL driver will convert all time values to Shanghai timezone (UTC+8) before executing the query. But the logger formats time values in their original timezone, resulting in different datetime string which could confuse people when debugging database related issues

Additional Notes

The mysql driver's behavior is documented: when parseTime=true and loc is set in the DSN, all time values are converted to the specified timezone before being sent to the database. GORM's logger should mirror this behavior to provide accurate SQL output.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions