所以我有一个现有的项目。我没有写任何这个,作者选择他们如何实现Slick让我感到困惑。
这是一个现有的表/光滑类集:
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文档,看起来我需要从对象引用一个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 {
}
}
答案 0 :(得分:0)
如果您想要的是拥有关系表
您可以建模如下
class PostTable(tag: Tag) extends Table[BlogPost](tag, "posts") {
def pid = column[Long]("pid", O.PrimaryKey, O.AutoInc)
def author = column[String]("author") // fk of user table
def userFK =
foreignKey("author_fk", author, TableQuery[UserTable])(_.email, ForeignKeyAction.Restrict, ForeignKeyAction.Cascade)
def * = (pid, author, title, content, postAt, tags) <> (BlogPost.tupled, BlogPost.unapply)
}
class UserTable(tag: Tag) extends Table[User](tag, "users") {
def email = column[String]("email", O.PrimaryKey)
def * = (email, password, name) <> (User.tupled, User.unapply)
}
注意PostTable中的userFK是一个fk约束
TableQuery
只是一个可用于查询数据库的对象
例如,您的代码中有val sources = TableQuery[SourcesRows]
,然后您可以
sources.filter(_.pid === 1001L)
表示select * from Sources where pid = 1001;
希望这个帮助=)