[Lift][Scala] モデルへの外部キー指定方法

移転しました。

Liftのメインページから、外部キー指定方法のドキュメントがたどれなかったので、メモっとく。
外部キー指定する方に、

  object 外部キー変数名 extends MappedLongForeignKey(this, 外部キーに指定するモデル)

を指定してやるだけでオK。

■外部キー設定される方

/**
 * The singleton that has methods for accessing the database
 */
trait MetaLCategory extends LCategory with KeyedMetaMapper[long, LCategory]
object LCategory extends MetaLCategory {
  override def dbTableName = "lcategories" // define the DB table name
}

class LCategory extends KeyedMapper[Long, LCategory] {
  def getSingleton = LCategory // what's the "meta" server
  def primaryKeyField = id
  
  // the primary key
  object id extends MappedLongIndex(this)
  
  object name extends MappedString(this, 100)
  object keywords extends MappedString(this, 255)
}

■外部キー設定する方

trait MetaArticle extends Article with KeyedMetaMapper[long, Article]
object Article extends MetaArticle {
  override def dbTableName = "articles" // define the DB table name
}

class Article extends KeyedMapper[Long, Article] {
  def getSingleton = Article // what's the "meta" server
  def primaryKeyField = id
  
  // the primary key
  object id extends MappedLongIndex(this)
  
  // column
  object title extends MappedString(this, 100)

  // forign key
  object category extends MappedLongForeignKey(this, LCategory)
}