1
votes

Have case class + slick table mapping.

a lot of classes use the same fields, like

class T1(tag: Tag) extends Table[caseClassA](tag, "A") {
  def id = column[Option[Long]]("ID", O.PrimaryKey, O.AutoInc)

  def id1 = column[Long]("ID1", O.NotNull)

  def id2 = column[String]("ID2", O.NotNull)

  def idn = column[String]("IDn", O.NotNull)
}


class T2(tag: Tag) extends Table[caseClassB](tag, "B") {
  def id = column[Option[Long]]("ID", O.PrimaryKey, O.AutoInc)

  def id1 = column[Long]("ID1", O.NotNull)

  def id2 = column[String]("ID2", O.NotNull)

  def idn = column[String]("IDn", O.NotNull)
}

How can I move id, id1, id2, idn to the root thread ?

Tried

trait BasicT extends Table {
...
}

without success, any ideas ?

BR!

3

3 Answers

0
votes

You can extract these fields into a trait, which itself does not have to extend Table.

trait BasicT {
  def id = column[Option[Long]]("ID", O.PrimaryKey, O.AutoInc)

  def id1 = column[Long]("ID1", O.NotNull)

  def id2 = column[String]("ID2", O.NotNull)

  def idn = column[String]("IDn", O.NotNull)
}

class T1(tag: Tag) extends Table[caseClassA](tag, "A") with BasicT

class T2(tag: Tag) extends Table[caseClassB](tag, "B") with BasicT
0
votes

To be able to use column your trait has to know that it will be mixed to Table instance:

trait BasicT {
  self: Table[_] =>

  def id = column[Option[Long]]("ID", O.PrimaryKey, O.AutoInc)

  def id1 = column[Long]("ID1", O.NotNull)

  def id2 = column[String]("ID2", O.NotNull)

  def idn = column[String]("IDn", O.NotNull)
}

class T1(tag: Tag) extends Table[caseClassA](tag, "A") with BasicT
class T2(tag: Tag) extends Table[caseClassB](tag, "B") with BasicT
0
votes

Considering the fact that the abstract Table actually takes parameters, maybe abstract class is a better a choice to unify the common fields.

abstract class BasicTable[T](tag: Tag, tableName: String) 
    extends Table[T](tag, tableName) {

    def id = column[Option[Long]]("ID", O.PrimaryKey, O.AutoInc)

    def id1 = column[Long]("ID1")

    def id2 = column[String]("ID2")

    def idn = column[String]("IDn")

}

class T1(tag: Tag) extends BasicTable[caseClassA](tag, "A") {
    ...
    override def *  = ...
}