2015-06-12 6 views
6

Ich stieß auf eine Bedingung, die für die Liste der Zahlen in Slick-3 zu filtern. Hier ist meine Entität.Filter für die Liste der Zahlen in Slick 3

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)) 

Hier mag ich für companyTypeId suchen und es ist Option[List[Long]]

ich von companyTable.filter(_.companyTypeId === Some(List(1,2))) versucht zu tun, aber immer Vector() und die Tabelle eine Reihe von Daten mit companyTypeId ist, wie {1} .Ich Postgres ist mit DB.

Vielen Dank im Voraus.

+0

Wie funktioniert die Einheit Blick in die Datenbank? Speichern Sie wirklich eine Liste von Werten in einer Spalte? –

+0

@Gregor Raýman ... ja es sieht aus wie {1,2,3} und der Datentyp ist bigint [] – Jet

+1

Interessant. Ich nehme an, es normalisieren, eine andere verknüpfte Tabelle ist keine Option für Sie. Ich bin mir nicht sicher, wie die eingebaute Unterstützung von Slick for Array-Spalten ist, aber dieses [thread] (https://groups.google.com/forum/#!topic/scalaquery/v7lhyCd8_6k) könnte hilfreich sein. –

Antwort

1

Mai werden Sie können es auf diese Weise versuchen:

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

@bhavya ..... Ich bekomme Typ Mismatch Fehler wie erwartet: Traversable [Liste [Long]] , tatsächlich: Traversable [Long] – Jet

+0

@Jet bearbeitet die Antwort – Bhavya

+0

@bhavya ... das ist das gleiche wie ich versuchte, wie in der Frage.Wenn die db hat die IDs {1,2} und ich gebe eine Suche wie CompanyTable. filter (_. companyTypeId inSet (Traversable (List (1,2)))) holt das Ergebnis, aber im Falle von ich sende companyTable.filter (_. companyTypeId inSet (Traversable (List (1)))) wird nicht ergeben jedes Ergebnis – Jet