Using Lift with Squeryl, how to I filter on a property of a parent class?

23 views
Skip to first unread message

Shafique Jamal

unread,
May 5, 2015, 12:51:34 PM5/5/15
to lif...@googlegroups.com
Hello all,

I am using lift with Squeryl, and I'm having trouble formulating a query. 

I have two tables: pkg and item. item has a foreign key to the pkg table. One pkg can have many items. Items in a pkg have a unique lineNumber (though items of different packages can have the same lineNumber) 

My model classes (I am I using the right terminology?) look like the following:

class Pkg extends Record[...

val id = ...
val pkgNumber = some string field
...
lazy val items = DBSchema.pkgToItems.left(this)

class Item extends Record[...

val id = ...
val lineNumber = StringField(...)
val pkgid = ForeignKeyField(...)
lazy val pkgs = DBSchema.pkgToItems.right(this)

Object DBSchema extends Schema {
val pkgs = table[Pkg]
val items = table[Item]
val pkgToItems = oneToManyRelation(pkgs, items).via((pkg, item) => pkg.id === item.pkgid)

}

Here is what I would like to do: given (a) a pkgNumber, and (b) a lineItem number, find the Item. 

Here is where I am stuck:

  def byLineNumber(pkgNumber:String, lineNumber:String):Option[Item] = {
    DBSchema.items.where(_.lineNumber === lineNumber)
      .where(XXX)
      .headOption
  } 

what should XXX look like? perhaps: _.pkgs.where(_.pkgNumber == pkgNumber).isDefined?

This doesn't look right, but I'm not sure. Any suggestions on the proper way to do this?

Many thanks,

David Whittaker

unread,
May 6, 2015, 2:51:51 PM5/6/15
to liftweb

Squeryl queries look very much like SQL. You can find more information and examples here: http://squeryl.org/selects.html. Similar to SQL, what you need is a where clause that contains two predicates concatenated with an and.

def byLineNumber(pkgNumber:String, lineNumber:String): Option[Item
] = {
    from(DBSchema.items)(i => where (i.pkgid === pkgNumber and i.lineNumber === lineNumber) select(i)).headOption

}

--
--
Lift, the simply functional web framework: http://liftweb.net
Code: http://github.com/lift
Discussion: http://groups.google.com/group/liftweb
Stuck? Help us help you: https://www.assembla.com/wiki/show/liftweb/Posting_example_code

---
You received this message because you are subscribed to the Google Groups "Lift" group.
To unsubscribe from this group and stop receiving emails from it, send an email to liftweb+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Shafique Jamal

unread,
May 6, 2015, 5:34:36 PM5/6/15
to lif...@googlegroups.com
Hello David,

Ah, I see now. Many thanks for this response.

Cheers,
> You received this message because you are subscribed to a topic in the
> Google Groups "Lift" group.
> To unsubscribe from this topic, visit
> https://groups.google.com/d/topic/liftweb/JraJHVYdpPY/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to
Reply all
Reply to author
Forward
0 new messages