我想使用Slick 3.0 ++ =函数并使用return来进行批量插入,以返回插入的对象。
我想知道返回对象(Future [Seq [Something]])是否与我的参数Seq [Something](无id)具有相同的顺序。
进一步来说,
val personList: Seq[Person] = Seq(Person("name1"), Person("name2"), Person("name3")) persons returning persons ++= personList
结果肯定是Future(Seq(Person(1,“ name1”),Person(2,“ name2”),Person(3,“ name3”))))吗?或者可以按其他结果顺序排列?
谢谢。
是的,我相信您正在使用自动递增的主键。我也和您提到的一样:
case class Person(name: String, id: Option[Int] = None) class PersonTable(tag: Tag) extends Table[Person](tag, "person") { val id = column[Int]("id", O.PrimaryKey, O.AutoInc) val name = column[String]("name") def * = (name, id.?) <> (Person.tupled, Person.unapply) } val personTableQuery = TableQuery[PersonTable] def personTableAutoIncWithObject = (personTableQuery returning personTableQuery.map(_.id)).into((person, id) => person.copy(id = Some(id))) // insert all person without id and return all person with their id. def insertAll(persons: List[Person]): Future[Seq[Person]] = db.run { personTableAutoIncWithObject ++= persons } //unit test for insertion order: test("Add new persons ") { val response = insertAll(List(Person("A1"), Person("A2"), Person("A3"), Person("A4"), Person("A5"))) whenReady(response) { persons => assert(persons === List(Person("A1", Some(1)), Person("A2", Some(2)), Person("A3", Some(3)), Person("A4", Some(4)), Person("A5", Some(5)))) } }