1

有点圆滑的 n00b ...我正在尝试构建一个插入语句,但发现我没有插入任何东西。

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 打印(使用所需的值),但不是 REACHED 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
 }
4

1 回答 1

4

在用户对象定义中,您需要更改:

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

于 2013-07-24T16:52:10.967 回答