1

I have an situation where one column is Bigint in one table and another one is varchar. I am using Scala and Slick for the application. The problem is I need to do a Left Join on these two columns, but getting compilation errors due to the type mismatch. I can change the DB column from BigInt to varchar, but then I will have to make changes in many places. Is there a way in which I can avoid changing the tables and the related entities ?

Yadu Krishnan
  • 3,492
  • 5
  • 41
  • 80

2 Answers2

1

You can cast the integer to string e.g.

LEFT JOIN mytable on myint::text = mytext

You can also use CAST(myint as varchar)

Have a look here.

kdauria
  • 6,300
  • 4
  • 34
  • 53
mlinth
  • 2,968
  • 6
  • 30
  • 30
1

You can cast a value of type Column using its method asColumnOf with a proper type parameter. This way the generated sql will also contain the cast in the join condition.

Something like:

for {
    (a, b) <- aTable.leftJoin(bTable).on(_.someColumn === _.anotherColumn.asColumnOf[Long])
} yield (a, b)
tfh
  • 620
  • 1
  • 4
  • 14