Skip to content
This repository has been archived by the owner on Sep 7, 2021. It is now read-only.
This repository is currently being migrated. It's locked while the migration is in progress.

problem xorm generate SQL stmt with WHERE when column name is SQL keyword #1324

Closed
huanjinzi opened this issue Jun 12, 2019 · 4 comments · Fixed by #1449
Closed

problem xorm generate SQL stmt with WHERE when column name is SQL keyword #1324

huanjinzi opened this issue Jun 12, 2019 · 4 comments · Fixed by #1449
Labels

Comments

@huanjinzi
Copy link

huanjinzi commented Jun 12, 2019

xorm生成的SQL语句:

SELECT `Id`, `Index`, `Platform` FROM `recommend` WHERE Index=? AND Platform=? LIMIT 1[1 1]

WHERE Index=? AND Platform=?

  • 正确 `Index`
  • 错误 Index,没有(`)

Index为关键字,所以SQL执行时候会有语法错误

@lunny
Copy link
Member

lunny commented Jun 12, 2019

Could you give your code here?

@huanjinzi
Copy link
Author

huanjinzi commented Jun 12, 2019

@lunny
when I execute code below:

engine := this.GetReadEngine()
engine.Where(query)

xorm will generate SQL statement with WHERE,in WHERE sub statement,column name don't quoted by (`).

Example

SELECT `Id`, `Index`, `Platform` FROM `recommend` WHERE Index=1 AND Platform=1

when column name is SQL keyword,such as SELECTINDEX, SQL statement execute with syntax error!

@huanjinzi huanjinzi changed the title xorm生成的SQL语句where子句问题 problem xorm generate SQL stmt with WHERE when column name is SQL keyword Jun 12, 2019
@lunny
Copy link
Member

lunny commented Jun 12, 2019

How is the query's struct's definition?

@huanjinzi
Copy link
Author

model struct

type Recommend struct {
	Id         int       `xorm:"not null pk autoincr comment('') INT(11)"`
	Index      int       `xorm:"not null comment('') INT(11)"`
	Platform   int       `xorm:"not null comment('') INT(11)"`
}
var r *Recommend
var query = map[string]interface{}{}
query["Platform"] = 1
query["Index"] = 1

engine := this.GetReadEngine()
engine.Where(query).Get(r)

execute above code, will see from xrom debug log:

SELECT `Id`, `Index`, `Platform` FROM `recommend` WHERE Index=? AND Platform=? [1 1]

because Index is SQL keyword, above SQL statement has syntax error.

when use:

query["`Platform`"] = 1
query["`Index`"] = 1

xorm SQL debug log is:

SELECT `Id`, `Index`, `Platform` FROM `recommend` WHERE `Index`=? AND `Platform`=? [1 1]

it works.

Is this a problem?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants