mysql两个表联合查询的时候,如果两个表有相同的字段名称会做成不匹配或者匹配错误。 #2192
Labels
No Label
backport/done
backport/v1
blocked
db
oracle
db
sqlserver
duplicate
feature
cache
frontport/done
frontport/main
invalid
kind
breaking
kind
bug
kind
build
kind
dependencies
kind
docs
kind
driver
kind
enhancement
kind
feature
kind
performance
kind
proposal
kind
question
kind
refactor
kind
testing
need
feedback
need
test
proposal:accepted
RaspBerry Pi
regression
skip-changelog
upstream
wip
wontfix
No Milestone
No Assignees
2 Participants
Notifications
Due Date
No due date set.
Dependencies
No dependencies set.
Reference: xorm/xorm#2192
Loading…
Reference in New Issue
Block a user
No description provided.
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
请问这个问题可以解决么?除了写sql的时候,指定重复字段用别名。还有其他更好的方法么?
type TeachingResource struct {
ID int64
xorm:"'id' bigint pk autoincr notnull"
// 主键Name string
xorm:"'name' varchar(256) notnull"
// 名称}
type ClassResource struct {
ID int64
xorm:"'id' bigint pk autoincr notnull"
// 主键Name string
xorm:"'name' varchar(256) notnull"
// 名称ResourceID int64
xorm:"'resource_id' bigint index"
// 资源ID}
type CombinedAdminCensoringProgress struct {
Resource *TeachingResource
xorm:"extends"
Myclass *ClassResource
xorm:"extends"
}
var rs = make([]*CombinedAdminCensoringProgress, 0)
err2 := engine.
SQL("SELECT teaching_resource.id,teaching_resource.name,teaching_resource.class_id,class_resource.id,class_resource.class_id FROM
teaching_resource
,class_resource
where teaching_resource.id = class_resource.resource_id").Find(&rs)
这样可以正确查询到希望的内容。但是如果CombinedAdminCensoringProgress的结构写发如下。
type CombinedAdminCensoringProgress struct {
Myclass *ClassResource
xorm:"extends"
Resource *TeachingResource
xorm:"extends"
}
这样查询结果的内容就不正确了。myclass和resource的id 和 name字段内容会对调了。这样是不正确的。有没有办法可以正确映射。
我跟踪过代码进入xorm的内部。发觉mysql返回的字段名称不带表前序。所以感觉这个bug无解。
我目前程序的情况比这个复杂很多。我最后debug的过程发现这个问题。我是因为新版本程序增加了字段,其中新增的一个字段和其他联合查询的表有相同的名字,但是字段类型不一样,导致旧版程序不修改代码的情况下出现映射失败。
extends顺序和join顺序保持一致就可以了。
你这个顺序一致就可以。我明白,我也知道。
我现在的问题是。后端程序升级的时候,希望新旧两个版本的程序同时运行,对应新旧两个版本的前端程序。
假设A B两个表联合查询,新版本程序 如果A表增加了一个字段和B表的字段同名。
那么旧版本的程序就会出问题。这样变了旧版本程序也要做适应性更新。
现实的情况表比较多,联合查询也多。然后这样每次数据库增加字段。都要新旧后台程序都要把所有功能都测试一遍,这样工作量大了很多。