Is there a way I can add comments into the sql that is formed by slick without writing a raw sql statement? This is to keep track of the code in application that launched the sql.
Asked
Active
Viewed 246 times
5
-
Could you give some example, exactly how you mean – Amit Prasad Dec 04 '18 at 18:12
-
Something similar to this https://www.techonthenet.com/postgresql/comments.php – binshi Dec 05 '18 at 16:31
2 Answers
3
I'm sorry to say no. Currently in the 3.3.1
version of Slick, there is no way to add a comment to queries other that creating the SQL code by hand.
It may be get added later as there is an open issue years ago for that:

Amoo Hesam
- 461
- 3
- 13
0
We accomplished this by downcasting DBIOAction and using SqlAction.overrideStatements:
val databaseAction: DBIOAction[A, NoStream, E] = ??? // your action
val comment: String = ??? // your comment
val annotatedAction = databaseAction match {
case sqlAction: SqlAction[A, NoStream, E] => sqlAction.overrideStatements(sqlAction.statements.map { s =>
"/* " + comment + " */ " + s
})
case otherAction => otherAction
}
db.run(annotatedAction)
It causes all SQL statements to be prefixed with a comment. You can make the comment be some unique identifier to help you connect SQL to your code, or you could do it automatically by finding a relevant stack frame in Thread.currentThread.getStackTrace
before running the query.

Jonathan Crosmer
- 786
- 5
- 19