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

Where(User{Name:"name"}) results in "where pets.name='name'" #6655

Open
gabriel-vasile opened this issue Oct 24, 2023 · 1 comment
Open

Where(User{Name:"name"}) results in "where pets.name='name'" #6655

gabriel-vasile opened this issue Oct 24, 2023 · 1 comment
Assignees
Labels
type:with reproduction steps with reproduction steps

Comments

@gabriel-vasile
Copy link

gabriel-vasile commented Oct 24, 2023

I'm not sure if this is a bug in gorm or just missing from documentation for breaking changes between gorm v1 and v2. This problem didn't exist in gorm v1.

When adding where conditions with structs, the table named is fixed to clause.CurrentTable regardless of the struct type. For example:

Where(Model1{ID: 1}).
Where(Model2{ID: 2}).
Where(Model3{ID: 3})

SQL:

wanted: where model1.id = 1 and model2.id = 2 and model3.id = 3
   got: where model1.id = 1 and model1.id = 2 and model1.id = 3

go-gorm/playground#656

Description

I am migrating from gorm v1 to gorm v2 and I found this difference that I don't think is documented in Breaking changes

stmt := DB.Session(&gorm.Session{DryRun: true}).
		Select("*").
		Joins("join users on users.id = pets.user_id").
		Where(&User{Name: "user_name"}).Find(&pets).Statement
expect := "SELECT * FROM `pets` join users on users.id = pets.user_id WHERE `users`.`name` = ? AND `pets`.`deleted_at` IS NULL"
got    := "SELECT * FROM `pets` join users on users.id = pets.user_id WHERE `pets`.`name` = ? AND `pets`.`deleted_at` IS NULL"
@gabriel-vasile
Copy link
Author

gabriel-vasile commented Nov 6, 2023

It seems like the issue was introduced in minor versions of v2 (not between v1 and v2, like I initially thought).

This issue was introduced in 3cd81ff. To me #3382 seems an invalid issue. The problem could have been solved by adding TableName method on RecordInfo, not by hardcoding all Where(Model{}) to the same table specified by Table().

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