From a1fe965302289ed7a0c89d31cd4ff88287ace463 Mon Sep 17 00:00:00 2001 From: pratap0007 Date: Wed, 8 Nov 2023 13:25:51 +0530 Subject: [PATCH] Remove subquery to fix uniqely reference users table Add users table name This fixes multiple times reference of same table in a query and which was creating conflict Signed-off-by: Shiv Verma --- api/pkg/auth/service/service.go | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/api/pkg/auth/service/service.go b/api/pkg/auth/service/service.go index 955b101c7b..5c296f0445 100644 --- a/api/pkg/auth/service/service.go +++ b/api/pkg/auth/service/service.go @@ -98,11 +98,14 @@ func createChecksum(token string) string { (Keeping email as the primary key in users table) We perform search query in users table on the basis of email: If Email is not present(existing user or new user) + query the accounts table on the basis of username and provider - If the above case matches -> update the email in users table and other details respectively in accounts table - If the above case returns empty record(new user) - create a new user in the database + If Email is already present + fetch the user_id from users table and query the accounts table with `where` clause of `user_id` and `provider` - if the record doesn't exists(user trying to login with new provider) then create a new record in accounts table with new provider and the user_id already associated with the email @@ -113,8 +116,8 @@ func (r *request) insertData(gitUser goth.User, code, provider string) error { var acc model.Account var user model.User - userQuery := r.db.Model(&model.User{}). - Where("email = ?", gitUser.Email) + userQuery := r.db.Table("users as u"). + Where("u.email = ?", gitUser.Email) // Check if user exist err := userQuery.First(&user).Error @@ -153,8 +156,8 @@ func (r *request) insertData(gitUser goth.User, code, provider string) error { } } else { // when the email of user already exists // Update the users table with the auth code - if err := userQuery.Update("code", code).Error; err != nil { - r.log.Error(err) + if err := r.db.Model(&model.User{}). + Where("email = ?", gitUser.Email).Update("code", code).Error; err != nil { return err }