MappedOneToMany

MappedOneToManyは、同じ2テーブル間で同じ方向に一対多関係を複数設置しても実体はMappedForeignKeyなのでどれも同じものとして扱われる。例えば、次のような設計はアンチパターン

class Counter extends LongKeyedMapper[Counter] with IdPK with OneToMany[Long, Counter] {
  def getSingleton = Counter
  object waiting extends MappedOneToMany(Invitation, Invitation.counter, OrderBy(Invitation.id, Ascending))
  object invited extends MappedOneToMany(Invitation, Invitation.counter, OrderBy(Invitation.id, Ascending))
//waitingとinvited以外のカラムがあればという意味
  object something_else extends MappedSomethingElse(this)
}
class Invitation extends LongKeyedMapper[Invitation] with IdPK {
  def getSingleton = Invitation
  object counter extends MappedLongForeignKey(this, Counter)
}
//以下のコードは読まなくても大丈夫
object Counter extends Counter with LongKeyedMetaMapper[Counter] {override def fieldOrder = List(id)}
object Invitation extends Invitation with LongKeyedMetaMapper[Invitation] {override def fieldOrder = List(id)}

対策としては、テーブルをわけてしまえばいい。

class WCounter extends LongKeyedMapper[WCounter] with IdPK with OneToMany[Long, WCounter] {
  def getSingleton = WCounter
  object waiting extends MappedOneToMany(Invitation, Invitation.wcounter, OrderBy(Invitation.id, Ascending))
//必要であれば
  object counter extends MappedLongForeignKey(this, Counter)
}
class ICounter extends LongKeyedMapper[ICounter] with IdPK with OneToMany[Long, ICounter] {
  def getSingleton = ICounter
  object invited extends MappedOneToMany(Invitation, Invitation.icounter, OrderBy(Invitation.id, Ascending))
//必要であれば
  object counter extends MappedLongForeignKey(this, Counter)
}
class Counter extends LongKeyedMapper[Counter] with IdPK with OneToMany[Long, Counter] {
  def getSingleton = Counter
//上のコード同様、waitingとinvited以外のカラムがなければCounterごと消す。
  object something_else extends MappedSomethingElse(this)
}
class Invitation extends LongKeyedMapper[Invitation] with IdPK {
  def getSingleton = Invitation
  object wcounter extends MappedLongForeignKey(this, WCounter)
  object icounter extends MappedLongForeignKey(this, ICounter)
  object counter extends MappedLongForeignKey(this, Counter)
}
//以下のコードは読まなくても大丈夫
object WCounter extends WCounter with LongKeyedMetaMapper[WCounter] {override def fieldOrder = List(id)}
object ICounter extends ICounter with LongKeyedMetaMapper[ICounter] {override def fieldOrder = List(id)}
object Counter extends Counter with LongKeyedMetaMapper[Counter] {override def fieldOrder = List(id)}
object Invitation extends Invitation with LongKeyedMetaMapper[Invitation] {override def fieldOrder = List(id)}