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

Gorm callbacks using raw() or native query not worked #7268

Closed
mftakhullaziz opened this issue Nov 7, 2024 · 1 comment
Closed

Gorm callbacks using raw() or native query not worked #7268

mftakhullaziz opened this issue Nov 7, 2024 · 1 comment
Assignees
Labels

Comments

@mftakhullaziz
Copy link

My Code

`
package main

  import (
      "context"
      "fmt"
      "gorm.io/driver/postgres"
      "gorm.io/gorm"
      "log"
      "posify-service/internal/identity/infra/psql/identity_records"
  )

  func RegisterRawQueryLogger(db *gorm.DB) {
      fmt.Println("Starting raw query logger")

      db.Callback().Raw().Before("gorm:raw").Register("my_query_logger_before", func(db *gorm.DB) {
	      log.Printf("Executing Query: %s", db.Statement.SQL.String())
	      log.Printf("Values: %v", db.Statement.Vars)
	      log.Printf("Qyert result: %v", db.Statement.Dest)
      })
  
      db.Callback().Raw().After("gorm:raw").Register("my_query_logger_after", func(db *gorm.DB) {
	      log.Printf("Executing Query: %s", db.Statement.SQL.String())
	      log.Printf("Values: %v", db.Statement.Vars)
	      log.Printf("Qyert result: %v", db.Statement.Dest)
      })
  }
  
  func GetRoleByCode(db *gorm.DB, roleCode string, ctx context.Context) (*identity_records.RoleRecord, error) {
      var role identity_records.RoleRecord
  
      // Execute raw query and scan result
      if err := db.WithContext(ctx).Raw("SELECT * FROM roles WHERE role_code = ?", roleCode).Scan(&role).Error; err != nil {
	      return nil, err
      }
      return &role, nil
  }
  
  func main() {
      // Set up database connection (PostgreSQL example DSN)
      dsn := "db connection"
      db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{})
      if err != nil {
	      log.Fatalf("failed to connect to database: %v", err)
      }
      ctx := context.Background()
  
      // Register the query logger
      RegisterRawQueryLogger(db)
  
      // Execute the query
      role, err := GetRoleByCode(db, "MERCHANT", ctx)
  
      if err != nil {
	      fmt.Println("Error executing query:", err)
      } else {
	      fmt.Printf("Query result main: %+v\n", role)
      }
  }

`

Description

I have issue, when I want printed query result from operation native query, I registered that both raw after and before, but from both even not triggered after query operation successful, please help me how fix that

@github-actions github-actions bot added the type:missing reproduction steps missing reproduction steps label Nov 7, 2024
Copy link

github-actions bot commented Nov 7, 2024

The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 30 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.ioSearch Before Asking

@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Dec 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants