3

I have a scala class with function named getUser which take a year & month. I am going to fetch a list of users with year & month based on birthday column which is defined as Instant.

I am using postgresql & slick. I wrote a sql like as:-

sql"SELECT * FROM user_info WHERE birthday::text LIKE '1990-07%'".as[Seq[User]]

For the above query I have also declare an implicit converter as follows:-

implicit val getUserResult = GetResult[User](r => User(r.<<, r.<<, r.<<, r.<<, r.<<))

I have written the corresponding code for the above query

case class User(id: Option[Int],
                name: String,
                birthday: Instant,
                updated: Option[Instant]) extends GenericEntity {
       def this(name: String,
                birthday: Instant) = this(None, name, birthday, None, None)
}

class UserTable(tag: Tag) extends GenericTable[User](tag, "user_info") {
      override def id = column[Option[Int]]("id", O.PrimaryKey, O.AutoInc)
      def name = column[String]("name")
      def birthday = column[Instant]("birthday")

  def * = (id, name, birthday, created, updated) <> ((User.apply _).tupled, User.unapply)
}

object Users extends GenericService[User, UserTable] {
      override val table = TableQuery[UserTable]

      override def copyEntityFields(entity: User, id: Option[Int], created: Option[Instant], updated: Option[Instant]): User = {
        entity.copy(id = id, created = created, updated = updated)
      }

      implicit val getUserResult = GetResult[User](r => User(r.<<, r.<<, r.<<, r.<<, r.<<))

      def userTable = "user_info"

      def getUsers(year: Int, month: Int) = {
        var monthString = month.toString
        if (month < 10)
          monthString = "0" + month.toString
        val yearMonth = year + "-" + monthString
        val sql = sql"SELECT * FROM #$userTable WHERE birthday::text LIKE '#$yearMonth%'".as[User]
        db.run(sql)
      }
}

When I am going to execute the query it introduces an error like (indicating with ^, that is the problem will introduce while it is trying to convert birthday):-

diverging implicit expansion for type slick.jdbc.GetResult[T1]
starting with object GetIntOption in object GetResult
implicit val getUserResult = GetResult[User](r => User(r.<<, r.<<, r.<<, r.<<, r.<<))
                                                                   ^

Let me know, how can I overcome the above problem?

testuser
  • 793
  • 3
  • 13
  • 35

0 Answers0