Question

I am trying to use squeryl and update a column with another value concatenated with a string. So at first I tried this (simplified/contrived example):

update(songs)(s =>
where(s.name isNull)
set(s.year_str := Some(s.default_year + " AD"))

This results in the query:

update `songs` set
`year_str` = (`songs`.`default_year`)
Where `songs`.`name` is null

Notice the concatenation is gone!

I read the squeryl documentation and tried with:

update(songs)(s =>
where(s.name isNull)
set(s.year_str := Some(&(s.default_year || " AD")))

this resulted in a NullPointerException

Caused by: java.lang.NullPointerException: null
at org.squeryl.internals.FieldReferenceLinker$.pushExpressionOrCollectValue(FieldReferenceLinker.scala:36) ~[squeryl_2.10-0.9.6-M1.jar:0.9.6-M1]
at org.squeryl.dsl.QueryDsl$class.$amp(QueryDsl.scala:204) ~[squeryl_2.10-0.9.6-M1.jar:0.9.6-M1]
...

What is the squeryl way to generate the following query:

update `songs` set
`year_str` = concat(`songs`.`default_year`, 'AD')
Where `songs`.`name` is null
Was it helpful?

Solution

You need to use the .~ operator, to convert the field "s.default_year" into an AST node, try this :

update(songs)(s =>
  where(s.name isNull)
  set(s.year_str := s.default_year.~ || " AD")
)

If you're using a pre 0.9.6 version of Squeryl you might have to do this instead :

set(s.year_str := s.default_year.~ || Some(" AD"))
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top