2016-11-29 8 views
1

私は以下のモデルに結合してDAOを実装しています。データベースに新しいエンティティを永続させるために(シリアルに生成されたIDを取得できるようにするための追加手順に注意してください)、これはうまくコンパイルされません。Slick 3.1.x CRUD:挿入された行IDを抽出する方法は?

// this is generated by the Slick codegen 
case class UserRow(id: Long, ... 
class User(_tableTag: Tag) extends Table[UserRow](_tableTag, "user") 
lazy val User = new TableQuery(tag => new User(tag)) 

// function to persist a new user 
def create(user: UserRow): Future[UserRow] = { 
    val insertQuery = User returning User.map(_.id) into ((row, id) => row.copy(id = id)) 
    val action = insertQuery += user 
    db.run(action) 
} 

は、今私はDAOは、一般的なモデルから切り離さ作ってみると(GenericDao.scalaで完全なソースコードを確認してください)があります。

def create(entity: E): Future[E] = { 
    val insertQuery = tableQuery returning tableQuery.map(_.id) into ((row, id) => row.copy(id = id)) 
    val action = insertQuery += entity 
    db.run(action) 
} 

をが、これはコンパイラエラーにつながる:

[error] /home/bravegag/code/play-authenticate-usage-scala/app/dao/GenericDao.scala:81: type mismatch; 
[error] found : GenericDao.this.driver.DriverAction[insertQuery.SingleInsertResult,slick.dbio.NoStream,slick.dbio.Effect.Write] 
[error]  (which expands to) slick.profile.FixedSqlAction[dao.Entity[PK],slick.dbio.NoStream,slick.dbio.Effect.Write] 
[error] required: slick.dbio.DBIOAction[E,slick.dbio.NoStream,Nothing] 
[error]  db.run(action) 
[error]   ^
[error] one error found 
[error] (compile:compileIncremental) Compilation failed 

なぜ戻り値の型が結合されたバージョンと違うのか、そしてそれを修正する方法/割り当てられたシリアルIDを持つ新しく作成されたエンティティを抽出する方法がわからない。

答えて

1

変更あなたの関数の戻り値の型Future[Entity[PK]]の代わりFuture[E]

def create(entity: E): Future[Entity[PK]] = { 
    val insertQuery = tableQuery returning tableQuery.map(_.id) into ((row, id) => row.copy(id = id)) 
    val action = insertQuery += entity 
    db.run(action) 
} 
関連する問題