Slick Scala没有插入任何值

时间:2013-07-24 16:43:52

标签: scala slick scalatra

一个光滑的n00b ...我正在尝试构建一个insert语句,但发现我没有插入任何内容。

def newUser(userName: String, email: Option[String], password: String = null, removed: Option[Int] = null) = SlickInit.dbMaster withSession {

  val creation = Option(new java.sql.Timestamp(new java.util.Date().getTime()))
  val last = new java.sql.Timestamp(new java.util.Date().getTime())

  printf("REACHED 1. creation: " + creation + "\n last: " + last)
  println("\nusername: " + userName + "\n email: " + email)
  println("maxId: " + maxId)

  val invoker = Users.insertInvoker

  (Users.userId ~ Users.userName ~ Users.email ~ Users.userPassword ~ Users.creationDate ~ Users.lastLoginDate ~ Users.removed).insert(maxId, userName, email, password, creation, last, removed)

  val statement = Users.insertStatement
  println("REACHED 2. \n\nStatement: " + statement)
}
发出POST请求时,

REACHED 1打印(带有所需的值)但是没有达到2.我确定我的插入语句有问题,但我不确定是什么。 (另外,显然,当我查询数据库时,插入的值不会返回。)有没有人对此有任何见解?

编辑,这是我的用户表定义:

object Users extends Table[(Int, String, Option[String], String, Option[Timestamp], Timestamp, Option[Int])]("APUsers") {

   def userId          = column[Int]("UserId", O.PrimaryKey, O.AutoInc)
   def userName        = column[String]("UserName")
   def email           = column[Option[String]]("Email", O.Nullable)
   def userPassword    = column[String]("UserPassword")
   def creationDate    = column[Option[Timestamp]]("CreationDate", O.Nullable)
   def lastLoginDate   = column[Timestamp]("LastLoginDate")
   def removed         = column[Option[Int]]("removed", O.Nullable)

   def * = userId ~ userName ~ email ~ userPassword ~ creationDate ~ lastLoginDate ~ removed
 }

1 个答案:

答案 0 :(得分:4)

在Users对象定义中,您需要更改它:

object Users extends Table[(Int, String, Option[String], String, Option[Timestamp], Timestamp, Option[Int])]("APUsers") {

到此:

object Users extends Table[(Option[Int], String, Option[String], String, Option[Timestamp], Timestamp, Option[Int])]("APUsers") {

因为id是由DBMS(O.AutoInc)分配的。请参阅此处的示例:http://slick.typesafe.com/doc/1.0.1/lifted-embedding.html#mapped-tables

然后你需要改变这个:

def * = userId ~ userName ~ email ~ userPassword ~ creationDate ~
        lastLoginDate ~ removed

到此:

def * = userId.? ~ userName ~ email.? ~ userPassword ~ creationDate.? ~
        lastLoginDate ~ removed.?

因为它们被定义为Option。见这里:http://slick.typesafe.com/doc/1.0.1/lifted-embedding.html#mapped-tables

newUser中,插入行应该是:

(Users.userId ~ Users.userName ~ Users.email ~ Users.userPassword ~
 Users.creationDate ~ Users.lastLoginDate ~ Users.removed)

(Users.userName ~ Users.email.? ~ Users.userPassword ~ Users.creationDate.? ~ 
 Users.lastLoginDate ~ Users.removed.?)
 .insert(userName, email, password, creation, last, removed)

没有userId,因为它将由DBMS分配。请参阅此处的示例:http://slick.typesafe.com/doc/1.0.1/lifted-embedding.html#inserting

由于您不接受数据库中的Null,我建议更改此内容:

def newUser(userName: String, email: Option[String], password: String = null, removed: Option[Int] = null)

到此:

def newUser(userName: String, email: Option[String], password: String, removed: Option[Int] = null)

并检查密码是否为空。

根据pedrofurla建议,您可以将以下内容添加到Users对象:

def forInsert = Users.userName ~ Users.email.? ~ Users.userPassword ~ 
                Users.creationDate.? ~ Users.lastLoginDate ~ Users.removed.?

并使该行更具可读性:

Users.forInsert.insert(userName, email, password, creation, last, removed)

请参阅http://slick.typesafe.com/doc/1.0.1/lifted-embedding.html#inserting