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

Question about Statement.table #771

Closed
YuzuWiki opened this issue Nov 13, 2024 · 0 comments
Closed

Question about Statement.table #771

YuzuWiki opened this issue Nov 13, 2024 · 0 comments
Assignees

Comments

@YuzuWiki
Copy link

GORM Playground Link

#1

Description

this issue about Statement.table . in some cases, it can lead to unexpected results.

Other issuse See: go-gorm/gorm#7280

Source Code SEE: LINE 493, https://github.com/go-gorm/gorm/blob/master/statement.go

Example:

  • exec sql
db, _ = gorm.Open(mysql.Open(dsn), &config)


#  use SQL 
type OtherTable struct {
   Name string 
}

var retSQL OtherTable 
db.Raw(`SELECT name FROM xxx JOIN  ..... LIMIT 1;`).Scan(&retSQL )

some time, db.statement.table will be set to other_table. now, db looks like this

db: {
     Statement:  {
           Table:    "other_table",
           Model:   nil,
          ........
     }
}

now,If we continue to execute

type User struct {
    ......
}

func (u User )  TableName()  string {
    return "user"
}


var u User 
db.Model(User{}).Where("id =?", uid).Find(&u)

maybe, we will get the following results

    SELECT * FROM `database`.other_table WHERE id = ? limit 1;

but our expected result is

    SELECT * FROM `database`.user WHERE id = ?;
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

No branches or pull requests

2 participants