Reputation: 93
I am trying to create a schema for a simple table in slick 3.2. I am pretty new to scala so this might be a stupid question. When I try to remove date projection seems to work fine and I am able to run select query fine , but when I add date , I am not able to even compile code .
Below is my schema code :
import slick.jdbc.OracleProfile.api._
import java.sql.Date
class User(tag: Tag)
extends slick.jdbc.OracleProfile.api.Table[(Long , String , Date)](tag, "USR") {
def usr_arch_dt : Rep[Date]= column[Date]("USR_ARCH_DT")
def usr_id : Rep[Long] = column[Long]("USR_ID", O.PrimaryKey)
def usr_subj_txt : Rep[String]= column[String]("USR_SUBJ_TXT")
def * : (Long, String, Date) =
(usr_id , usr_subj_txt , usr_arch_dt) // I see mentioned error here
}
Exception or error I see is in Intellij is :
Expression of type Rep[Long] doesn't conform to expected type Long.
If I am able to compile this code , I am looking to search based on date range something like
val filterQuery: Query[User, (Long, String , Date), Seq] =
ntfns.filter(_.usr_arch_dt > Calendar.getInstance().getTime )
Thank you !
Upvotes: 0
Views: 108
Reputation: 93
Thank you @Leo, extra String was a typo, I removed it from the question now for others benefit. Below code worked for me :
case class User(tag: Tag)
extends slick.jdbc.OracleProfile.api.Table[(Long , String , Date)](tag, "USR") {
def usr_arch_dt : Rep[Date]= column[Date]("USR_ARCH_DT")
def usr_id : Rep[Long] = column[Long]("USR_ID", O.PrimaryKey)
def usr_subj_txt : Rep[String]= column[String]("USR_SUBJ_TXT")
def * = (usr_id , usr_subj_txt , usr_arch_dt)
}
I still need to look at filter, I will try your method and let you know the outcome.
Upvotes: 0
Reputation: 22449
Looks like your def * : (Long, String, String, Date)
has an extra String
.
On date search, your filter query won't work as java.sql.Date
comes with limited methods. A common practice in Slick is to create an implicit mapper between java.sql.Date
and the more feature-rich Joda Time:
import scala.slick.driver.JdbcProfile.MappedColumnType
import java.sql.Date
import org.joda.time.DateTime
// ...
implicit def dateTimeMapper = MappedColumnType.base[DateTime, Date] (
dt => new Date(dt.getMillis),
d => new DateTime(d)
)
Here's a related SO link.
Upvotes: 1