Slick 3.0.0 max query

507 views
Skip to first unread message

Joe San

unread,
May 21, 2015, 9:58:52 AM5/21/15
to scala...@googlegroups.com

I'm trying to do a complex querying on two tables. They look like below:

Table1:
  id:
  name:
  table2Id:
  version:

Table2:
  id:
  comments:

Assuming that I have the appropriate Slick classes that represents these tables, I'm trying to get all the elements from Table1 and Table 2 that satisfies the following condition:

Table1.table2Id === Table2.id and max(Table1.version)

I tried the following:

val groupById = (for {
  elem1 <- table1Elems
  elem2 <- table2Elems if elem1.id === elem2.id
} yield (elem1, elem2)).groupBy(_._1.id)

I know that I have to map the groupById and look for the max version, but I'm not getting the syntax right! Any help?

Joe San

unread,
May 22, 2015, 3:43:30 AM5/22/15
to scala...@googlegroups.com
Here is what I have so far:

val groupById = (for {
elem1 <- table1Elems
elem2 <- table2Elems if elem1.id === elem2.id
} yield (elem1, elem2)).groupBy(_._1.id)

I will then use sortBy to sort the elements in descending order and take the first one:

val orderBy = groupById.map {
case (id, grouped) => grouped.sortBy(x => x._1.version.desc).take(1)
}.result

When I tried to compile this, I ran into the following error:

Error:(105, 33) No matching Shape found.
Slick does not know how to map the given types.
Possible causes: T in Table[T] does not match your * projection. Or you use an unsupported type in a Query (e.g. scala List).
 
Required level: slick.lifted.FlatShapeLevel
     
Source type: slick.lifted.Query[(ServiceTests.this.myService.Table1, ServiceTests.this.myService.Table2),((String, String, String, String, String, Int, java.sql.Date, java.sql.Date), (String, String, String, Boolean)),[+A]Seq[A]]
   
Unpacked type: T
     
Packed type: G
    val orderBy
= groupById.map {

                                ^

Joe San

unread,
May 22, 2015, 8:39:57 PM5/22/15
to scala...@googlegroups.com
Here is another attempt:

I'm trying to get the max version number from a table. Here is the sql that I came up with:

SELECT * from table1 t1
WHERE t1.conficVersion = 
  (SELECT max(configVersion) FROM table2 t2 WHERE t2.id = t1.id)

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

table1.groupBy(p => p.id)
  .map { case (id, group) => (id, group.map(_.version).max) }

Now how can I combine this grouped result into a proper query that will actually fetch me the max version for all the id's? Please help!

Reply all
Reply to author
Forward
0 new messages