gpt4 book ai didi

mysql - Slick 2.1.0 和 Traits 中的外键关系

转载 作者:行者123 更新时间:2023-11-29 20:58:14 24 4
gpt4 key购买 nike

所以我有一个现有的项目。这些都不是我写的,作者选择如何实现 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 {

}

}

最佳答案

如果你想要的是有一个有关系的表

您可以按如下方式建模

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 只是一个可用于查询数据库的对象

例如,您的代码中有 valsources = TableQuery[SourcesRows],然后您就可以执行此操作

sources.filter(_.pid === 1001L) 

这意味着从 pid = 1001 的源中选择*;

希望这有帮助 =)

关于mysql - Slick 2.1.0 和 Traits 中的外键关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37448137/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com