How can I have more than one join with the same table

63 views Asked by At

In Scala anorm with MySQL, it seems joining more than once the same table in a query and working with aliases only returns the value of the last table included in the query.

It seems the last join automatically overrides all the previous values even though it has a different alias.

See example below.

Say I have a table mytable

id | name
1  | name1
2  | name2

And in mymaintable a record: myId1 = 1 and myId2 = 2

If I do

SELECT
t1.*, t2.*
FROM mymaintable AS m
JOIN mytable AS t1 ON t1.id = m.myId1
JOIN mytable AS t2 ON t2.id = m.myId2

I would expect 1, name1, 2, name2

Now in Anorm i would define aliaser so that

val aliaser = new ColumnAliaser {
      def as1 = ColumnAliaser.withPattern((0 to 2).toSet, "t1.")
      def as2 = ColumnAliaser.withPattern((2 to 4).toSet, "t2.")

      def apply(column: (Int, ColumnName)): Option[String] = as1(column).orElse(as2(column))
    }

and the result would be 2, name2, 2, name2

Is this a bug? Am I doing anything wrong?

It might be worth noting that I use anorm 2.5.3

0

There are 0 answers