Scala 更新和删除在play slick中不起作用的查询

Scala 更新和删除在play slick中不起作用的查询,scala,playframework,crud,slick,Scala,Playframework,Crud,Slick,我正试图为play slick编写一个简单的CRUD应用程序。我可以插入数据库并显示所有用户,但不能更新或删除用户。我的全部代码如下 package models import javax.inject.{ Inject, Singleton } import play.api.db.slick.DatabaseConfigProvider import slick.jdbc.JdbcProfile import scala.concurrent.{ Future, ExecutionCon

我正试图为play slick编写一个简单的CRUD应用程序。我可以插入数据库并显示所有用户,但不能更新或删除用户。我的全部代码如下

package models

import javax.inject.{ Inject, Singleton }
import play.api.db.slick.DatabaseConfigProvider
import slick.jdbc.JdbcProfile

import scala.concurrent.{ Future, ExecutionContext }

/**
 * A repository for people.
 *
 * @param dbConfigProvider The Play db config provider. Play will inject this for you.
 */
@Singleton
class PersonRepository @Inject() (dbConfigProvider: DatabaseConfigProvider)(implicit ec: ExecutionContext) {
  // We want the JdbcProfile for this provider
  private val dbConfig = dbConfigProvider.get[JdbcProfile]

  // These imports are important, the first one brings db into scope, which will let you do the actual db operations.
  // The second one brings the Slick DSL into scope, which lets you define the table and other queries.
  import dbConfig._
  import profile.api._

  /**
   * Here we define the table. It will have a name of people
   */
  private class PeopleTable(tag: Tag) extends Table[Person](tag, "people") {

    /** The ID column, which is the primary key, and auto incremented */
    def id = column[Long]("id", O.PrimaryKey, O.AutoInc)

    /** The name column */
    def name = column[String]("name")

    /** The age column */
    def age = column[Int]("age")

    /**
     * This is the tables default "projection".
     *
     * It defines how the columns are converted to and from the Person object.
     *
     * In this case, we are simply passing the id, name and page parameters to the Person case classes
     * apply and unapply methods.
     */
    def * = (id, name, age) <> ((Person.apply _).tupled, Person.unapply)
  }

  /**
   * The starting point for all queries on the people table.
   */
  private val people = TableQuery[PeopleTable]

  /**
   * Create a person with the given name and age.
   *
   * This is an asynchronous operation, it will return a future of the created person, which can be used to obtain the
   * id for that person.
   */
  def create(name: String, age: Int): Future[Person] = db.run {
    // We create a projection of just the name and age columns, since we're not inserting a value for the id column
    (people.map(p => (p.name, p.age))
      // Now define it to return the id, because we want to know what id was generated for the person
      returning people.map(_.id)
      // And we define a transformation for the returned value, which combines our original parameters with the
      // returned id
      into ((nameAge, id) => Person(id, nameAge._1, nameAge._2))
    // And finally, insert the person into the database
    ) += (name, age)
  }

  /**
   * List all the people in the database.
   */
  def list(): Future[Seq[Person]] = db.run {
    people.result
  }

  def del(id: Int) : Future[Seq[Person]] = db.run {
   people.filter(_.id === id).delete

  }

  def update (id: Int, name: String) : Future[Seq[Person]] = {
    db.run(people.filter(_.id === id).update("john"))

  }
}
包模型
import javax.inject.{inject,Singleton}
导入play.api.db.slick.DatabaseConfigProvider
导入slick.jdbc.jdbc配置文件
导入scala.concurrent.{Future,ExecutionContext}
/**
*一个供人们使用的存储库。
*
*@param dbConfigProvider播放数据库配置提供程序。Play将为您注入此功能。
*/
@独生子女
类PersonRepository@Inject()(dbConfigProvider:DatabaseConfigProvider)(隐式ec:ExecutionContext){
//我们需要此提供程序的JdbcProfile
private val dbConfig=dbConfigProvider.get[JdbcProfile]
//这些导入很重要,第一个导入将db引入范围,这将允许您执行实际的db操作。
//第二种方法将Slick DSL引入范围,允许您定义表和其他查询。
导入dbConfig_
导入profile.api_
/**
*在这里我们定义这个表。它将有一个人名
*/
私有类PeopleTable(tag:tag)扩展表[Person](tag,“people”){
/**ID列,它是主键,并自动递增*/
def id=列[Long](“id”,O.PrimaryKey,O.AutoInc)
/**“名称”列*/
def名称=列[字符串](“名称”)
/**年龄栏*/
定义年龄=列[Int](“年龄”)
/**
*这是表格的默认“投影”。
*
*它定义了如何在Person对象之间转换列。
*
*在本例中,我们只是将id、name和page参数传递给Person case类
*应用和取消应用方法。
*/
def*=(id、姓名、年龄)((Person.apply..tupped,Person.unapply)
}
/**
*人员表上所有查询的起点。
*/
private val people=TableQuery[PeopleTable]
/**
*创建具有给定姓名和年龄的人。
*
*这是一个异步操作,它将返回所创建人员的未来,可用于获取
*那个人的身份证。
*/
def create(名称:String,年龄:Int):Future[Person]=db.run{
//我们只创建name和age列的投影,因为我们没有为id列插入值
(people.map(p=>(p.name,p.age))
//现在定义它以返回id,因为我们想知道为此人生成了什么id
返回的people.map(u.id)
//我们为返回值定义了一个转换,它将原始参数与
//返回id
into((nameAge,id)=>Person(id,nameAge.\u 1,nameAge.\u 2))
//最后,将此人插入数据库
)+=(姓名、年龄)
}
/**
*列出数据库中的所有人员。
*/
def list():Future[Seq[Person]=db.run{
人。结果
}
def del(id:Int):Future[Seq[Person]]=db.run{
people.filter(u.id==id).删除
}
def更新(id:Int,name:String):未来[Seq[Person]={
db.run(people.filter(u.id==id.update(“john”))
}
}
也可能我排除了一些导入,所以请检查是否需要导入任何内容

我也遇到了这个错误,我不知道这意味着什么:

类型失配;发现: UserData.this.dbConfig.profile.ProfileAction[Int,slick.dbio.NoStream,slick.dbio.Effect.Write] (扩展至) slick.sql.FixedSqlAction[Int,slick.dbio.NoStream,slick.dbio.Effect.Write] 必修的: slick.dbio.DBIOAction[Seq[models.User],slick.dbio.NoStream,Nothing]


我认为当您删除或更新时,返回类型有问题。 它们返回已修改的行数

试试这个:

def del(id: Int) : Future[Int] = db.run {
  people.filter(_.id === id).delete
}

def update (id: Int, name: String) : Future[Int] = {
  db.run(people.filter(_.id === id).update("john"))
}

请指定行errors@prms,def update(id:Int,name:String):Future[Seq[Person]={db.run(people.filter(u.id==id.update(“john”))}