Sujit Sujit - 1 year ago 432
Scala Question

How to fix Slick Exception of single AutoInc column to be returned from an INSERT

I tried to implement the akka-http rest example provided at
but I'm stuck with the

[ERROR] [08/28/2016 10:35:34.091] [] [] Error during processing of request HttpRequest(HttpMethod(POST),,List(Host:, Connection: keep-alive, Cache-Control: no-cache, Origin: chrome-extension://fhbjgbiflinjbdggehcddcbncdddomop, User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36, Postman-Token: ec513598-b8bc-87ca-8eba-743b214fe1fa, Accept: */*, Accept-Encoding: gzip, deflate, Accept-Language: en-US, en;q=0.8, Timeout-Access: <function1>),HttpEntity.Strict(application/json, {
"login": "admin",
"password": "admin"
slick.SlickException: This DBMS allows only a single AutoInc column to be returned from an INSERT
at slick.driver.JdbcStatementBuilderComponent$JdbcCompiledInsert.buildReturnColumns(JdbcStatementBuilderComponent.scala:69)
at slick.driver.JdbcActionComponent$ReturningInsertActionComposerImpl.x$12$lzycompute(JdbcActionComponent.scala:633)
at slick.driver.JdbcActionComponent$ReturningInsertActionComposerImpl.x$12(JdbcActionComponent.scala:633)
at slick.driver.JdbcActionComponent$ReturningInsertActionComposerImpl.keyColumns$lzycompute(JdbcActionComponent.scala:633)
at slick.driver.JdbcActionComponent$ReturningInsertActionComposerImpl.keyColumns(JdbcActionComponent.scala:633)
at slick.driver.JdbcActionComponent$ReturningInsertActionComposerImpl.preparedInsert(JdbcActionComponent.scala:636)
at slick.driver.JdbcActionComponent$InsertActionComposerImpl$
at slick.driver.JdbcActionComponent$
at slick.driver.JdbcActionComponent$
at slick.backend.DatabaseComponent$DatabaseDef$$anon$2.liftedTree1$1(DatabaseComponent.scala:237)
at slick.backend.DatabaseComponent$DatabaseDef$$anon$
at java.util.concurrent.ThreadPoolExecutor.runWorker(
at java.util.concurrent.ThreadPoolExecutor$

Here is the Scala Code:
Signup API:

path("signUp") {
pathEndOrSingleSlash {
post {
entity(as[UserEntity]) { userEntity =>
complete(Created -> signUp(userEntity).map(_.asJson))


package oc.api.models.db

* Created by sujit on 8/27/16.
import oc.api.models.UserEntity
import oc.api.utils.DatabaseService
trait UserEntityTable {
protected val databaseService: DatabaseService
import databaseService.driver.api._

class Users(tag: Tag) extends Table[UserEntity](tag, "users") {
def id = column[Option[Long]]("id", O.PrimaryKey, O.AutoInc)
def username = column[String]("username")
def password = column[String]("password")

def * = (id, username, password) <> ((UserEntity.apply _).tupled, UserEntity.unapply)

protected val users = TableQuery[Users]


package oc.api.models

* Created by sujit on 8/27/16.
case class UserEntity(id: Option[Long] = None, username: String, password: String) {
require(!username.isEmpty, "username.empty")
require(!password.isEmpty, "password.empty")

case class UserEntityUpdate(username: Option[String] = None, password: Option[String] = None) {
def merge(user: UserEntity): UserEntity = {
UserEntity(, username.getOrElse(user.username), password.getOrElse(user.password))



import oc.api.models.{TokenEntity, UserEntity}
import oc.api.models.db.TokenEntityTable
import oc.api.utils.DatabaseService

import scala.concurrent.{ExecutionContext, Future}

* Created by sujit on 8/27/16.
class AuthService(val databaseService: DatabaseService)(usersService: UsersService)(implicit executionContext: ExecutionContext) extends TokenEntityTable {

import databaseService._
import databaseService.driver.api._

def signUp(newUser: UserEntity): Future[TokenEntity] = {
usersService.createUser(newUser).flatMap(user => createToken(user))

def authenticate(token: String): Future[Option[UserEntity]] = {
token <- tokens.filter(_.token === token)
user <- users.filter( === token.userId)
} yield user).result.headOption)

def createToken(user: UserEntity): Future[TokenEntity] = returning tokens += TokenEntity(userId =


Since I'm new to Scala and Slick, can anyway provide the information why this exception is occurring even though I've defined
in Model

Answer Source

I assumed your TokenEntity looks something like this where id is auto incremented and token is automatically created:

case class UserEntity(id: Option[Long] = None, userId: Long, token: String)

Now you are asking your DBMS to return multiple columns (id, userId and token) in your insert action which is not supported. Change

def createToken(user: UserEntity): Future[TokenEntity] = returning tokens += TokenEntity(userId =


def createToken(user: UserEntity): Future[TokenEntity] =
  tokens returning into((token, id) => token.copy(id = Some(id)) += TokenEntity(userId =

Here you specify column id to be returned on insert. The following into method merges token and the generated key. See Inserting for a more detailed explanation.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download