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

Statement.table has a BUG. #7280

Open
YuzuWiki opened this issue Nov 13, 2024 · 2 comments
Open

Statement.table has a BUG. #7280

YuzuWiki opened this issue Nov 13, 2024 · 2 comments
Assignees
Labels

Comments

@YuzuWiki
Copy link

YuzuWiki commented Nov 13, 2024

GORM Playground Link

Description

Statement.table. If it exists and is an unexpected value, it may cause the query to not meet expectations.

  • Source Code:

https://github.com/go-gorm/gorm/blob/master/statement.go

lool this: LINE 493, err == nil && stmt.Table == ""

func (stmt *Statement) ParseWithSpecialTableName(value interface{}, specialTableName string) (err error) {
	if stmt.Schema, err = schema.ParseWithSpecialTableName(value, stmt.DB.cacheStore, stmt.DB.NamingStrategy, specialTableName); err == nil && stmt.Table == "" {
		if tables := strings.Split(stmt.Schema.Table, "."); len(tables) == 2 {
			stmt.TableExpr = &clause.Expr{SQL: stmt.Quote(stmt.Schema.Table)}
			stmt.Table = tables[1]
			return
		}

		stmt.Table = stmt.Schema.Table
	}
	return err
}

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 = ?;
@github-actions github-actions bot added the type:missing reproduction steps missing reproduction steps label Nov 13, 2024
Copy link

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

Copy link

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

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