Skip to content

IDs of child table is invalid when update in MySQL #805

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

isutare412
Copy link

@isutare412 isutare412 commented Jun 5, 2025

Explain your user case and expected results

❯ GORM_DIALECT=mysql go test
2025/06/05 22:53:08 testing mysql...

2025/06/05 22:53:08 /Users/redacted/gorm-issue-report/main_test.go:21
[1.735ms] [rows:1] INSERT INTO `envs` (`app_id`,`key`,`value`) VALUES (1,'KEY1','value1') ON DUPLICATE KEY UPDATE `app_id`=VALUES(`app_id`)

2025/06/05 22:53:08 /Users/redacted/gorm-issue-report/main_test.go:21
[6.075ms] [rows:1] INSERT INTO `apps` (`name`) VALUES ('foo')

2025/06/05 22:53:08 /Users/redacted/gorm-issue-report/main_test.go:35
[1.140ms] [rows:3] INSERT INTO `envs` (`app_id`,`key`,`value`) VALUES (1,'KEY1','value1-updated'),(1,'KEY2','value2-added') ON DUPLICATE KEY UPDATE `app_id`=VALUES(`app_id`),`key`=VALUES(`key`),`value`=VALUES(`value`)

2025/06/05 22:53:08 /Users/redacted/gorm-issue-report/main_test.go:35
[4.826ms] [rows:0] UPDATE `apps` SET `name`='foo' WHERE `id` = 1

2025/06/05 22:53:08 /Users/redacted/gorm-issue-report/main_test.go:42
[0.941ms] [rows:2] SELECT * FROM `envs` WHERE `envs`.`app_id` = 1

2025/06/05 22:53:08 /Users/redacted/gorm-issue-report/main_test.go:42
[2.121ms] [rows:1] SELECT * FROM `apps` WHERE `apps`.`id` = 1 ORDER BY `apps`.`id` LIMIT 1
--- FAIL: TestGORM (0.01s)
    main_test.go:22: appInitial: &{ID:1 Name:foo Envs:[0x1400037b2f0]}
    main_test.go:24:   env[0]: &{ID:1 AppID:1 Key:KEY1 Value:value1}
    main_test.go:36: appUpdated: &{ID:1 Name:foo Envs:[0x140002a3e90 0x140002a3ec0]}
    main_test.go:38:   env[0]: &{ID:2 AppID:1 Key:KEY1 Value:value1-updated}
    main_test.go:38:   env[1]: &{ID:3 AppID:1 Key:KEY2 Value:value2-added}
    main_test.go:43: appFetched: &{ID:1 Name:foo Envs:[0x140002f28a0 0x140002f2930]}
    main_test.go:45:   env[0]: &{ID:1 AppID:1 Key:KEY1 Value:value1-updated}
    main_test.go:45:   env[1]: &{ID:2 AppID:1 Key:KEY2 Value:value2-added}
    main_test.go:59: Expected env ID 1, got 2
    main_test.go:59: Expected env ID 2, got 3
FAIL
exit status 1
FAIL	gorm.io/playground	1.132s

Environment

  • MySQL

Symptom

IDs after Update() is not same as those saved in database.
As you see, IDs in database is 1, 2 but IDs of updated results are 2, 3.

appUpdated: &{ID:1 Name:foo Envs:[0x140002a3e90 0x140002a3ec0]}
  env[0]: &{ID:2 AppID:1 Key:KEY1 Value:value1-updated}
  env[1]: &{ID:3 AppID:1 Key:KEY2 Value:value2-added}
appFetched: &{ID:1 Name:foo Envs:[0x140002f28a0 0x140002f2930]}
  env[0]: &{ID:1 AppID:1 Key:KEY1 Value:value1-updated}
  env[1]: &{ID:2 AppID:1 Key:KEY2 Value:value2-added}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant