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

需要绑定的变量对应的tag未生效 #136

Closed
jun0315 opened this issue Mar 4, 2021 · 2 comments
Closed

需要绑定的变量对应的tag未生效 #136

jun0315 opened this issue Mar 4, 2021 · 2 comments
Labels
good first issue Good for newcomers

Comments

@jun0315
Copy link

jun0315 commented Mar 4, 2021

image
运行示例代码,如果把user对应的age和uid属性调换一下时,即使绑定了对应的tag,但是仍然按照数据库中顺序进行赋值,数据库的第一列uid对应的了user类的第一个属性age

@jun0315
Copy link
Author

jun0315 commented Mar 4, 2021

image
在源码中session.go的scanStructAll函数中,rows.Scan(sfs...)这一行调试发现,运行之后对应的sfs和s.IBinder.BindResult对应的变量直接按照数据库查询的列顺序绑定了值,未按照tag进行比对,不知道是不是feature还是bug

@fizzday fizzday added the good first issue Good for newcomers label Mar 8, 2021
@fizzday
Copy link
Member

fizzday commented Mar 8, 2021

在orm中, 其实是先解析struct, 再把解析的字段用于查询, 所以, 会造成这种结果.
而这个原生查询绑定是外挂到原生查询的, 所以, 要按照struct顺序才行, 这个是database/sql底层所决定的, 也就是需要人工手动指定的
原生查询是

...
var field1 stirng
var field2 int
rows.scan(&field1, &field2)
...

从这个原生构造中就可以看出, 程序自己是无法确定哪个字段的, 除非舍弃一定的性能, 先把rows.scan绑定到map, 然后再用map去映射, 即可解决顺序问题, 这里需要额外浪费一倍的空间和性能

gorose在这里做了取舍, 用了手动指定顺序, 看来需要在文档里说明一下, 免得造成疑惑

@fizzday fizzday closed this as completed Mar 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

2 participants