【问题标题】:Foreign key relationship in Slick 2.1.0 and TraitsSlick 2.1.0 和 Traits 中的外键关系
【发布时间】:2016-05-25 21:44:11
【问题描述】:

所以我有一个现有的项目。我没有写任何这些,作者对他们如何实现 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 {

  }

}

【问题讨论】:

    标签: mysql scala slick-2.0


    【解决方案1】:

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

    你可以如下建模

    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;

    希望这个帮助 =)

    【讨论】:

    • 并非如此。如果您查看我的问题,您会发现我有一个特征,其中包含类以及特征中的伴随对象。如果我尝试实现您的示例,则 ListsRows 仍然无法访问。我不能写:"foreignKey("fk_item_list_id", listId, TableQuery[ListsRows])(_.id)"。
    • 另外,您的示例毫无价值,因为它只是一个选择。它甚至不加入外键。
    • 如果你的问题是有类内部特征,对我来说这似乎是错误的,但解决方案是使用 # 运算符来访问它,你可以通过 ItemsTable#ItemsRows 访问 ItemsRows,你的问题说与加入无关,加入只是您可以在 Slick API 文档中找到的另一种方法。
    猜你喜欢
    • 1970-01-01
    • 2018-01-10
    • 1970-01-01
    • 2015-10-11
    • 2012-02-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-08
    相关资源
    最近更新 更多