Slick 3 - how to get correct (database) schema when inserting with plain SQL

156 Views Asked by At

I'm trying to get basic plain SQL example working in Slick 3, on Postgres but with custom DB schema, say local instead of default public one. I have hard time inserting the row as executing the following

sqlu"INSERT INTO schedule(user_id, product_code, run_at)  VALUES ($userId, $code, $nextRun)"

says

org.postgresql.util.PSQLException: ERROR: relation "schedule" does not exist

The table is in place because when I prefix schedule with local. in the insert statement it works as expected. How can I get correct schema provided to this query?

I'm using it as part of akka-projection handler and all the projection internals like maintaining offsets work as expected on local schema.

I cannot simply put schema as a variable as it errors while resolving parameters:

sqlu"INSERT INTO ${schema}.schedule(user_id, product_code, run_at)  VALUES ($userId, $code, $nextRun)"
1

There are 1 best solutions below

1
kardapoltsev On

You can insert schema name using #${value}:

sqlu"INSERT INTO #${schema}.table ..."