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

Unscoped preload all not working #397

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

Conversation

antoninbas
Copy link

@antoninbas antoninbas commented Nov 4, 2021

Explain your user case and expected results

I want to use Unscoped with a "preload all" to load all associations, but it is not working as expected.

The following fails to load associations which have been soft-deleted:

DB.Preload(clause.Associations, func(db *gorm.DB) *gorm.DB { return db.Unscoped() }).Find(&user, id)

While the following (load specific associations by name) succeeds:

DB.Preload("Company", func(db *gorm.DB) *gorm.DB { return db.Unscoped() }).Preload("Manager", func(db *gorm.DB) *gorm.DB { return db.Unscoped() }).Find(&user, id)

Notice the differences in SQL queries:

=== RUN   TestGORM/Distinct_preloads

2021/11/04 12:55:47 /Users/abas/vmware/gorm-playground/main_test.go:45
[0.283ms] [rows:1] SELECT * FROM `companies` WHERE `companies`.`id` = 1

2021/11/04 12:55:47 /Users/abas/vmware/gorm-playground/main_test.go:45
[0.276ms] [rows:1] SELECT * FROM `users` WHERE `users`.`id` = 1

2021/11/04 12:55:47 /Users/abas/vmware/gorm-playground/main_test.go:45
[1.571ms] [rows:1] SELECT * FROM `users` WHERE `users`.`id` = 2 AND `users`.`deleted_at` IS NULL
=== RUN   TestGORM/Preload_all

2021/11/04 12:55:47 /Users/abas/vmware/gorm-playground/main_test.go:62
[0.149ms] [rows:0] SELECT * FROM `accounts` WHERE `accounts`.`user_id` = 2 AND `accounts`.`deleted_at` IS NULL

2021/11/04 12:55:47 /Users/abas/vmware/gorm-playground/main_test.go:62
[0.240ms] [rows:0] SELECT * FROM `companies` WHERE `companies`.`id` = 1 AND `companies`.`deleted_at` IS NULL

2021/11/04 12:55:47 /Users/abas/vmware/gorm-playground/main_test.go:62
[0.127ms] [rows:0] SELECT * FROM `user_friends` WHERE `user_friends`.`user_id` = 2

2021/11/04 12:55:47 /Users/abas/vmware/gorm-playground/main_test.go:62
[0.120ms] [rows:0] SELECT * FROM `user_speaks` WHERE `user_speaks`.`user_id` = 2

2021/11/04 12:55:47 /Users/abas/vmware/gorm-playground/main_test.go:62
[0.119ms] [rows:0] SELECT * FROM `users` WHERE `users`.`id` = 1 AND `users`.`deleted_at` IS NULL

2021/11/04 12:55:47 /Users/abas/vmware/gorm-playground/main_test.go:62
[0.119ms] [rows:0] SELECT * FROM `pets` WHERE `pets`.`user_id` = 2 AND `pets`.`deleted_at` IS NULL

2021/11/04 12:55:47 /Users/abas/vmware/gorm-playground/main_test.go:62
[0.121ms] [rows:0] SELECT * FROM `users` WHERE `users`.`manager_id` = 2 AND `users`.`deleted_at` IS NULL

2021/11/04 12:55:47 /Users/abas/vmware/gorm-playground/main_test.go:62
[0.134ms] [rows:0] SELECT * FROM `toys` WHERE `owner_type` = "users" AND `toys`.`owner_id` = 2 AND `toys`.`deleted_at` IS NULL

2021/11/04 12:55:47 /Users/abas/vmware/gorm-playground/main_test.go:62
[2.554ms] [rows:1] SELECT * FROM `users` WHERE `users`.`id` = 2 AND `users`.`deleted_at` IS NULL
    main_test.go:66: Wrong company name
    main_test.go:69: Wrong manager

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.

None yet

1 participant