Slick 2.1.0 Filter Max Version in a Table

那年仲夏 提交于 2019-12-22 00:29:17

问题


I'm trying to get the max version number from a table. My table contents are:

id externalId name version
1  10         n1   1
2  65         n2   2
3  10         n3   2
4  77         n4   1 

In the table above, we have rows that has the maximum version as 2, so my query has to return this maximum version row for the given externalId that I pass into my query.

I'm now stuck up with writing the Slick version of it. Any suggestions, pointers? This is what I have so far: (I pass in the externalId)

for { 
myTable1 <- myTable1Elems
    if myTable1.version === (
      myTable1Elems
        .filter(_.externalId === s"$externalId")
        .map(_.version)
        .max
  )
} yield myTable1

This seems to be doing the wrong thing, as my tests show that it returns 2 rows when I ask the maximum version for externalId = 77. I get both

1  10         n1   1
4  77         n4   1

returned as results which definitely is wrong. I guess I need to groupBy, but I just could not figure out how I could write the Slick combinator! Any help?


回答1:


So I managed to get this solved:

private def configMaxVersionFilter(extId: String) = for {
  tbl <- myTable1Elems
  if tbl.externalId === s"$extId" && tbl.version === (
    myTable1Elems
      .filter(_.externalId === s"$extId")
      .map(_.version)
      .max
    )
} yield tbl


来源:https://stackoverflow.com/questions/30407289/slick-2-1-0-filter-max-version-in-a-table

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!