带有 "in" 查询的 GORM where 闭包删除生成的 SQL 中的 where 子句
GORM's where closure with an "in" Query Drops the where clause in generated SQL
源代码: https://github.com/HybridProgrammer/GormIn
Grails 版本:3.2.8
我正在尝试 运行 以下查询:
def query = UserData.where {
teams { id in teamIds }
}
当 运行在 Spock 测试中查询时,它工作正常。当从 Grails 服务中 运行 连接它时,它 returns UserData 中的所有行,并且不会仅过滤掉属于 teamIds 列表中的团队的那些用户。
Spock 测试
运行 来自 Spock 测试的查询 - 始终通过
void "never fails - direct approach"() {
given:
setupData()
def me = User.first()
def teamIds = me.getAuthorities().id
when:
def query = UserData.where {
teams { id in teamIds }
}
then:
teamIds.size() == 1
query.size() == 2
}
生成SQL
select count(*) as y0_
from user_data this_ inner join workflow_role_teams teams3_
on this_.id=teams3_.user_data_teams_id inner join role teams_alia1_
on teams3_.role_id=teams_alia1_.id
where teams_alia1_.id in (?)
limit ?
运行 来自 Grails 服务的查询
偶尔通过 IntelliJ,从不形成 CLI
void "sometimes fails"() {
given:
setupData()
def me = User.first()
when:
def query = exampleService.getMyOrMyTeamsData(me)
then:
me.getAuthorities().size() == 1
query.size() == 2
}
exampleService.getMyOrMyTeamsData
def getMyOrMyTeamsData(User me) {
def teamIds = me.getAuthorities().id
def query = UserData.where {
teams { id in teamIds }
}
return query
}
生成SQL
select count(*) as y0_
from user_data this_ inner join workflow_role_teams teams3_
on this_.id=teams3_.user_data_teams_id inner join role teams_alia1_
on teams3_.role_id=teams_alia1_.id
limit ?
解决方案
请参阅解决方案分支
https://github.com/HybridProgrammer/GormIn/commit/d510e63b3e85b82ce06e24d8a392c10873a8306c
这里回答了问题:
在grails-app/domain/oneisone/UserData.groovy
List teams = new ArrayList()
改为
List<Role> teams = new ArrayList()
运行 grails clean 然后重新运行测试。
源代码: https://github.com/HybridProgrammer/GormIn
Grails 版本:3.2.8
我正在尝试 运行 以下查询:
def query = UserData.where {
teams { id in teamIds }
}
当 运行在 Spock 测试中查询时,它工作正常。当从 Grails 服务中 运行 连接它时,它 returns UserData 中的所有行,并且不会仅过滤掉属于 teamIds 列表中的团队的那些用户。
Spock 测试
运行 来自 Spock 测试的查询 - 始终通过
void "never fails - direct approach"() {
given:
setupData()
def me = User.first()
def teamIds = me.getAuthorities().id
when:
def query = UserData.where {
teams { id in teamIds }
}
then:
teamIds.size() == 1
query.size() == 2
}
生成SQL
select count(*) as y0_
from user_data this_ inner join workflow_role_teams teams3_
on this_.id=teams3_.user_data_teams_id inner join role teams_alia1_
on teams3_.role_id=teams_alia1_.id
where teams_alia1_.id in (?)
limit ?
运行 来自 Grails 服务的查询
偶尔通过 IntelliJ,从不形成 CLI
void "sometimes fails"() {
given:
setupData()
def me = User.first()
when:
def query = exampleService.getMyOrMyTeamsData(me)
then:
me.getAuthorities().size() == 1
query.size() == 2
}
exampleService.getMyOrMyTeamsData
def getMyOrMyTeamsData(User me) {
def teamIds = me.getAuthorities().id
def query = UserData.where {
teams { id in teamIds }
}
return query
}
生成SQL
select count(*) as y0_
from user_data this_ inner join workflow_role_teams teams3_
on this_.id=teams3_.user_data_teams_id inner join role teams_alia1_
on teams3_.role_id=teams_alia1_.id
limit ?
解决方案
请参阅解决方案分支 https://github.com/HybridProgrammer/GormIn/commit/d510e63b3e85b82ce06e24d8a392c10873a8306c
这里回答了问题:
在grails-app/domain/oneisone/UserData.groovy
List teams = new ArrayList()
改为
List<Role> teams = new ArrayList()
运行 grails clean 然后重新运行测试。