Scala Slick MySQL - performing advanced SQL stuff

195 views Asked by At

I have 3 tables in my MySQL db: applications, users and application_images. I need to write a query that will generate the next SQL:

SELECT * FROM applications 
JOIN users ON applications.user_id=users.id 
LEFT JOIN applications_images ON applications_images.app_id=applications.id
WHERE applications.id=?

How can this be achieved with the slick syntax?

And on the same subject: How can I write plain SQL queries with slick (on MySQL DB)?

1

There are 1 answers

1
Ben Reich On BEST ANSWER

When using leftJoin, you use the ? method to project the table to an Option. Yours might look something like:

applications join users on (_.user_id === _.id) map { 
    case (app, user) => (app, user) 
} leftJoin application_images on (_._1.id === _.app_id) map {
    case ((app, user), image) => (app, user, image.?)
} filter(_._1.id === {id})