【发布时间】:2020-09-06 18:23:59
【问题描述】:
由于第一个表中的外键,我需要对三个表进行 INNER JOIN 如下:
CREATE TABLE "product" (
"id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
"name" VARCHAR NOT NULL,
"price" FLOAT NOT NULL,
"categoryid" INT NOT NULL,
"supplierid" INT NOT NULL,
FOREIGN KEY(categoryid) references category(id),
FOREIGN KEY(supplierid) references supplier(id)
);
在模型中我有方法来处理所有列表:
def list(): Future[Seq[(Product, Category, Supplier)]] = db.run {
productTable.join(categoryTable).on(_.categoryid === _.id).join(supplierTable).on(_._1.supplierid === _.id).result
}
但这会返回嵌套元组而不是平面元组:((Product, Category), Supplier)。 那么我应该如何加入这些表以获得扁平元组,或者如果不能这样做,我该如何扁平化这个元组?
编辑:
实际上,我发现的唯一对我有用的解决方案是手动使用地图:
def list(): Future[Seq[(Product, Category, Supplier)]] = db.run {
productTable.join(categoryTable).on(_.categoryid === _.id).join(supplierTable).on(_._1.supplierid === _.id).result.map(a => Seq((a(1)._1._1,a(1)._1._2,a(1)._2)))
}
看起来和感觉都很糟糕。但到目前为止只有这个有效...... 有更好的想法吗?
【问题讨论】:
-
这能回答你的问题吗? multiple joins with slick
-
@AlleXyS:你提供的答案给出了我现在得到的嵌套元组的结果