RichyHBM
RichyHBM

Reputation: 798

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?

Upvotes: 2

Views: 1524

Answers (2)

Moritz
Moritz

Reputation: 559

For everybody landing on this question now (with Slick 3.2 and higher): The return type is not Future[Int] but Future[Option[Int]]. Reason for this is that not all databases actually return a number of inserted row. The documentation describes it here.

So the code in newer versions of Slick must look like this:

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

Upvotes: 0

TheKojuEffect
TheKojuEffect

Reputation: 21101

++= 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)
}

Upvotes: 4

Related Questions