Search code examples
scalaplayframeworkslick

Insert Sequence into Database, Slick 3


I want to add a method to my DAOs to allow me to insert a list of elements rather than a single element, for the single element I have:

def add(userGroup: UserGroup): Future[Int] = {
  dbConfig.db.run(userGroups += userGroup)
}

The documentation seems to suggest I should be able to use ++= like so

def add(userGroups: Seq[UserGroup]): Future[Int] = {
  dbConfig.db.run(userGroups ++= userGroups)
}

http://slick.typesafe.com/doc/3.0.0/queries.html#inserting

But this doesn't work, the ++= isn't what it expects apparently?


Solution

  • ++= added a sequence to TableQuery. From your second snippet, it looks like you are just adding userGroups: Seq[UserGroup] to itself rather than TableQuery instance.

    If your first snippet works, adding userGroups: Seq[UserGroup] to this.userGroups could work.

    def add(userGroups: Seq[UserGroup]): Future[Int] = {
      dbConfig.db.run(this.userGroups ++= userGroups)
    }