0
私は既存のプロジェクトを持っています。私はこれを書いていませんでした。そして、彼らがSlickをどのように実装したかについての著者の選択は、私を幾分混乱させます。ここで Slick 2.1.0とTraitsの外来キーの関係
は、クラスの既存のテーブル/スリックセットですcase class SourcesRow(id: Long,
childSourceId: Long,
childSourceName: String,
parentSourceId: Long,
parentSourceName: String)
trait SourcesTable { this : DbProfile =>
import profile.simple._
class SourcesRows(tag : Tag) extends Table[SourcesRow](tag, "Sources") {
def id = column[Long]("Id", O.PrimaryKey, O.NotNull, O.AutoInc)
def childSourceId = column[Long]("ChildSourceId", O.NotNull)
def childSourceName = column[String]("ChildSourceName", O.NotNull)
def parentSourceId = column[Long]("ParentSourceId", O.NotNull)
def parentSourceName = column[String]("ParentSourceName", O.NotNull)
def * = (id, childSourceId, childSourceName, parentSourceId, parentSourceName) <> (SourcesRow.tupled, SourcesRow.unapply)
}
val sources = TableQuery[SourcesRows]
object SourcesTable {
def listSources()(implicit session: SessionDef) =
sources.run
}
}
...と、私たちは今、私は」そう
class ControlledValuesDb(override val profile: JdbcProfile) extends DbProfile
with RestrictionsTable
with RestrictionCategoriesTable
with SourcesTable
with CollectionsTable
with SiteDestinationsTable
with SupplementalCategoriesTable
with ListsTable
with ItemsTable {
...
}
のようなデータベースオブジェクトにロードされているそれらのいくつか持っています私はSlick 2.1のドキュメントを見てきましたが、オブジェクトから1つのTableQueryを参照する必要があるように見えますが、どのように関係があるのかこれを達成するには、以下をご覧ください。
case class ItemsRow(id: Long , listId: Long, value: String)
case class ListsRow(id: Long, name: String)
trait ListsTable { this: DbProfile =>
import profile.simple._
class ListsRows(tag: Tag) extends Table[ListsRow](tag, "Lists") {
def id = column[Long]("Id", O.PrimaryKey, O.NotNull, O.AutoInc)
def name = column[String]("Name", O.NotNull)
def * = (id, name) <> (ListsRow.tupled, ListsRow.unapply)
}
val lists = TableQuery[ListsRows]
object ListsTable {
}
}
trait ItemsTable { this: DbProfile =>
import profile.simple._
class ItemsRows(tag : Tag) extends Table[ItemsRow](tag, "Items") {
def id = column[Long]("Id", O.PrimaryKey, O.NotNull, O.AutoInc)
def listId = column[Long]("ListId", O.NotNull)
def value = column[String]("Val", O.NotNull)
//def list = foreignKey("fk_item_list_id", listId, ???)(_.id)
def * = (id, listId, value) <> (ItemsRow.tupled, ItemsRow.unapply)
}
val items = TableQuery[ItemsRows]
object ItemsTable {
}
}
そうでもありません。あなたが私の質問を見たなら、私はそのクラス内のクラスとその特性内のコンパニオンオブジェクトを持っていることがわかります。あなたの例を実装しようとすると、ListsRowsはまだアクセスできません。私は書くことができません: "foreignKey(" fk_item_list_id "、listId、TableQuery [ListsRows])(_。id)"。 – djsumdog
また、あなたの例は単なる選択であるため、かなり無価値です。それは外部キーと結合しません。 – djsumdog
あなたの問題がクラス内の特性を持っているなら、それは私にとっては間違っているようですが、解決策は#演算子を使ってアクセスし、ItemsTree#ItemsRowsでItemsRowsにアクセスできます。 Slick APIのドキュメントにある別の方法です。 – Qingwei