Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I correctly compare Options members in Slick?

I'm doing things with Addresses, and the member subpremise(apartment/condo #) causes retrieves to miss. I also have concerns about subpremise being a part of my unique index constraint, given it can be null.

Failure filter:

tableQuery.filter(c=> (c.longitude === r.longitude && c.latitude === r.latitude) ||
        (c.streetNumber === r.streetNumber && c.route === r.route && c.subpremise === r.subpremise && c.neighborhoodId === r.neighborhoodId))

Successful filter: (by removung subpremise)

tableQuery.filter(c=> (c.longitude === r.longitude && c.latitude === r.latitude) ||
            (c.streetNumber === r.streetNumber && c.route === r.route && c.neighborhoodId === r.neighborhoodId)) 

I've included the definitions below s.t. if there is another contributing factor that I've missed, hopefully it will be noticed.

case class Address(id:Option[Long],streetNumber:Short,route:String,subpremise:Option[String],neighborhoodId:Fk,latitude:Option[Double],longitude:Option[Double])

class Addresses(tag: Tag) extends Table[Address](tag, "addresses") with Logging {
  def id = column[Long]("id", O.PrimaryKey, O.AutoInc)
  def streetNumber = column[Short]("street_number")
  def route = column[String]("route",O.NotNull)
  def subpremise = column[Option[String]]("subpremise")
  def neighborhoodId = column[Long]("neighborhood",O.NotNull)
  def latitude = column[Option[Double]]("latitude")
  def longitude = column[Option[Double]]("longitude")

  //Constraints
  def idx = index("idx_streetnum_route_subpremise_neighborhood", (streetNumber,route,subpremise,neighborhoodId), unique = true)
  def gps = index("gps", (latitude,longitude), unique = true)

  //Foreign Key
  def neighborhood = foreignKey("NEIGHBORHOOD_FK", neighborhoodId, Neighborhoods.tableQuery)(_.id)

  def * = (id.?,streetNumber,route,subpremise,neighborhoodId,latitude,longitude) <> (Address.tupled,Address.unapply)
}
like image 608
jordan3 Avatar asked Jun 24 '14 15:06

jordan3


1 Answers

The answer was to use the following check.

( //Option Scenario both are defined
  (c.subpremise.isDefined && r.subpremise.isDefined && c.subpremise === r.subpremise) ||
  //Option Scenario both are empty
  (c.subpremise.isEmpty && r.subpremise.isEmpty)
)
like image 125
jordan3 Avatar answered Sep 18 '22 21:09

jordan3