Filter for the list of numbers in slick 3

2.8k views Asked by At

I came across a condition that to filter for the list of numbers in slick-3. Here is my entity.

    case class Company(id:Long, name: String, companyTypeId: Option[List[Long]] = None)

    implicit def GetResultCompany(implicit e0: GR[Long], e1: GR[String], e2: GR[Option[List[Long]]]): GR[Company] = GR{
        prs => import prs._
          CompanyTest.tupled((<<[Long], <<[String],<<?[List[Long]]))
      }
    class CompanyTable(_tableTag: Tag) extends Table[Company](_tableTag, Some("base"), "Company") {
        def * = (id, name,companyTypeId) <> (CompanyTest.tupled, CompanyTest.unapply)
        def ? = (Rep.Some(id), Rep.Some(name), companyTypeId).shaped.<>({r=>import r._; _1.map(_=> CompanyTest.tupled((_1.get, _2.get _3)))}, (_:Any) =>  throw new Exception("Inserting into ? projection not supported."))
        val id: Rep[Long] = column[Long]("CompanyId", O.AutoInc, O.PrimaryKey)
        val name: Rep[String] = column[String]("Name", O.Length(250,varying=true))
        val companyTypeId: Rep[Option[List[Long]]] = column[Option[List[Long]]]("CompanyTypeId", O.Length(19,varying=false), O.Default(None))
      }
    lazy val companyTable = new TableQuery(tag => new CompanyTable(tag))

Here i want to search for companyTypeId and it is Option[List[Long]]

I tried doing by companyTable.filter(_.companyTypeId === Some(List(1,2))) but getting Vector() and the table is having a row of data with companyTypeId as {1}.I am using Postgres DB.

Thanks in advance.

1

There are 1 answers

7
Bhavya Latha Bandaru On

May be you can try it this way :

companyTable.filter(_.companyTypeId inSet(Traversable(List(1,2)))) 
// the above query filters rows with values in the given list