Closed
Description
GORM Playground Link
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 = ?;
Metadata
Metadata
Assignees
Labels
No labels