Scala Slick 3.0.1 与自我的关系
Scala Slick 3.0.1 Relationship to self
我有一个名为类别的实体,它与自身有关系。有两种类别,parent 类别和子类别。子类别在 idParent 属性中具有来自 parent 类别的 id。
我是这样定义架构的
class CategoriesTable(tag: Tag) extends Table[Category](tag, "CATEGORIES") {
def id = column[String]("id", O.PrimaryKey)
def name = column[String]("name")
def idParent = column[Option[String]]("idParent")
def * = (id, name, idParent) <> (Category.tupled, Category.unapply)
def categoryFK = foreignKey("category_fk", idParent, categories)(_.id.?)
def subcategories = TableQuery[CategoriesTable].filter(_.id === idParent)
}
我有这个数据:
id name idParent
------------------------------
parent Parent
child1 Child1 parent
child2 Child2 parent
现在我想在按 parent 类别分组的地图中获取结果,例如
Map(
(parent,Parent,None) -> Seq[(child1,Child1,parent),(child2,Child2,parent]
)
为此,我尝试了以下查询:
def findChildrenWithParents() = {
db.run((for {
c <- categories
s <- c.subcategories
} yield (c,s)).sortBy(_._1.name).result)
}
如果此时我执行查询:
categoryDao.findChildrenWithParents().map {
case categoryTuples => categoryTuples.map(println _)
}
我明白了:
(Category(child1,Child1,Some(parent)),Category(parent,Parent,None))
(Category(child2,Child2,Some(parent)),Category(parent,Parent,None))
这里有两个事实已经让我感到不安:
- 它返回的是 Future[Seq[Category, Category]] 而不是我期望的 Future[Seq[Category, Seq[Category]]]。
顺序颠倒了,我希望 parent 先出现,例如:
(类别(parent,Parent,None),类别(child1,Child1,Some(parent)))
(类别(parent,Parent,None),类别(child2,Child2,Some(parent)))
现在我会尝试将它们分组。因为我在 Slick 中遇到嵌套查询问题。我对这样的结果进行分组:
categoryDao.findChildrenWithParents().map {
case categoryTuples => categoryTuples.groupBy(_._2).map(println _)
}
但是结果真的很乱:
(Category(parent,Parent,None),Vector((Category(child1,Child1,Some(parent)),Category(parent,Parent,None),(Category(child2,Child2,Some(parent)),Category(parent,Parent,None))))
我本以为:
(Category(parent,Parent,None),Vector(Category(child1,Child1,Some(parent)),Category(child2,Child2,Some(parent))))
你能帮我解决倒置结果和分组依据吗?
提前致谢。
好的,我设法自己修复了它。如果有人想从中学习,请在这里回答:
def findChildrenWithParents() = {
val result = db.run((for {
c <- categories
s <- c.subcategories
} yield (c,s)).sortBy(_._1.name).result)
result map {
case categoryTuples => categoryTuples.groupBy(_._1).map{
case (k,v) => (k,v.map(_._2))
}
}
}
解决方案并不完美。我想通过已经在 Slick 中创建组,但这会检索到我想要的内容。
我有一个名为类别的实体,它与自身有关系。有两种类别,parent 类别和子类别。子类别在 idParent 属性中具有来自 parent 类别的 id。
我是这样定义架构的
class CategoriesTable(tag: Tag) extends Table[Category](tag, "CATEGORIES") {
def id = column[String]("id", O.PrimaryKey)
def name = column[String]("name")
def idParent = column[Option[String]]("idParent")
def * = (id, name, idParent) <> (Category.tupled, Category.unapply)
def categoryFK = foreignKey("category_fk", idParent, categories)(_.id.?)
def subcategories = TableQuery[CategoriesTable].filter(_.id === idParent)
}
我有这个数据:
id name idParent
------------------------------
parent Parent
child1 Child1 parent
child2 Child2 parent
现在我想在按 parent 类别分组的地图中获取结果,例如
Map( (parent,Parent,None) -> Seq[(child1,Child1,parent),(child2,Child2,parent] )
为此,我尝试了以下查询:
def findChildrenWithParents() = {
db.run((for {
c <- categories
s <- c.subcategories
} yield (c,s)).sortBy(_._1.name).result)
}
如果此时我执行查询:
categoryDao.findChildrenWithParents().map {
case categoryTuples => categoryTuples.map(println _)
}
我明白了:
(Category(child1,Child1,Some(parent)),Category(parent,Parent,None))
(Category(child2,Child2,Some(parent)),Category(parent,Parent,None))
这里有两个事实已经让我感到不安:
- 它返回的是 Future[Seq[Category, Category]] 而不是我期望的 Future[Seq[Category, Seq[Category]]]。
顺序颠倒了,我希望 parent 先出现,例如:
(类别(parent,Parent,None),类别(child1,Child1,Some(parent))) (类别(parent,Parent,None),类别(child2,Child2,Some(parent)))
现在我会尝试将它们分组。因为我在 Slick 中遇到嵌套查询问题。我对这样的结果进行分组:
categoryDao.findChildrenWithParents().map {
case categoryTuples => categoryTuples.groupBy(_._2).map(println _)
}
但是结果真的很乱:
(Category(parent,Parent,None),Vector((Category(child1,Child1,Some(parent)),Category(parent,Parent,None),(Category(child2,Child2,Some(parent)),Category(parent,Parent,None))))
我本以为:
(Category(parent,Parent,None),Vector(Category(child1,Child1,Some(parent)),Category(child2,Child2,Some(parent))))
你能帮我解决倒置结果和分组依据吗?
提前致谢。
好的,我设法自己修复了它。如果有人想从中学习,请在这里回答:
def findChildrenWithParents() = {
val result = db.run((for {
c <- categories
s <- c.subcategories
} yield (c,s)).sortBy(_._1.name).result)
result map {
case categoryTuples => categoryTuples.groupBy(_._1).map{
case (k,v) => (k,v.map(_._2))
}
}
}
解决方案并不完美。我想通过已经在 Slick 中创建组,但这会检索到我想要的内容。