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

Updating UpdateAt with .Save does not use the given value #7238

Open
jpenna opened this issue Oct 14, 2024 · 0 comments
Open

Updating UpdateAt with .Save does not use the given value #7238

jpenna opened this issue Oct 14, 2024 · 0 comments
Assignees
Labels
type:critical critical questions

Comments

@jpenna
Copy link

jpenna commented Oct 14, 2024

GORM Playground Link

I will add the link later, can't do it now, but I think the description is enough to understand the issue:
https://github.com/go-gorm/playground

Description

I'm using the following code to create a thread_update. See that I'm passing the UpdatedAt value.

	property := models.ThreadProperty{
		ThreadID:  threadId,
		Key:       key,
		Value:     value,
		UserID:    thread.UserID,
		UpdatedAt: time.Now().UTC(),
	}

	if err := dbClient.ThreadProperty.Save(&property); err != nil {
		return fmt.Errorf("failed to save property [key: %v | value: %v]: %w", key, value, err)
	}

The generated SQL is as below. See that the UpdatedAt passed to VALUES is 12:55, which is correctly set to UTC, but the UPDATE SET clause for updated_at has my local time 09:55.

INSERT INTO
    "thread_properties" (
        "thread_id",
        "key",
        "value",
        "metadata",
        "updated_at",
        "user_id"
    )
VALUES
    (
        '059f5bd0-531a-4752-9587-99c2f42518a7',
        'relevance',
        '{"relevance":"urgent","description":"stub desc","why_matters":"stub why matters"}',
        NULL,
        '2024-10-14 12:55:44.891', # This is UTC and the value I passed
        'cb3e9edc-9c5f-406d-820f-d4496f821d2e'
    ) ON CONFLICT ("thread_id", "key")
DO
UPDATE
SET
    "updated_at" = '2024-10-14 09:55:44.891', # This isn't UTC and not the value I passed in
    "value" = "excluded"."value",
    "metadata" = "excluded"."metadata",
    "user_id" = "excluded"."user_id"

Expectation

It should be using the value I passed in for UpdatedAt both for in VALUES and UPDATE SET.

"Quick-fix" (not exactly...)

If you have a the same problem with UpdatedAt, you can pass a function to gorm.Open to use UTC (or whatever timezone) as a configuration:

       instance, err := gorm.Open(postgres.Open(dsn), &gorm.Config{
		NowFunc: func() time.Time {
			return time.Now().UTC()
		},
	})
@github-actions github-actions bot added the type:critical critical questions label Oct 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type:critical critical questions
Projects
None yet
Development

No branches or pull requests

2 participants