8000 Question about Statement.table · Issue #771 · go-gorm/playground · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
Question about Statement.table  #771
Closed
@YuzuWiki

Description

@YuzuWiki

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 = ?;

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions

    0