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 #4836

Closed
antoninbas opened this issue Nov 4, 2021 · 0 comments
Closed

Unscoped "Preload All" not working #4836

antoninbas opened this issue Nov 4, 2021 · 0 comments
Assignees
Labels
type:with reproduction steps with reproduction steps

Comments

@antoninbas
Copy link

GORM Playground Link

go-gorm/playground#397

Description

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

The Unscoped is basically ignored for the "Preload All" case, with deleted_at IS NULL` being added to queries.

@github-actions github-actions bot added the type:with reproduction steps with reproduction steps label Nov 4, 2021
@jinzhu jinzhu closed this as completed in ca7accd Nov 8, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type:with reproduction steps with reproduction steps
Projects
None yet
Development

No branches or pull requests

2 participants