Domanda

Sort of a slick n00b...I'm trying to build an insert statement, but find I am not inserting anything.

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)
}

REACHED 1 prints (with the desired values) when a POST request is issued, but not REACHED 2. I'm sure theres something wrong with my insert statement, but I'm not sure what. (Also, obviously, when I query the database, the inserted value does not return.) Does anyone have any insight into this?

EDIT, here is my Users table definition:

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
 }
È stato utile?

Soluzione

In the Users object definition, you need to change this:

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

to this:

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

because the id is assigned by the DBMS (O.AutoInc). See the example here: http://slick.typesafe.com/doc/1.0.1/lifted-embedding.html#mapped-tables

Then you need to change this:

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

to this:

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

because they are defined as Option. See here: http://slick.typesafe.com/doc/1.0.1/lifted-embedding.html#mapped-tables

In the newUser the insert line should be, from this:

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

to

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

without userId, because it'll be assigned by the DBMS. See the example here: http://slick.typesafe.com/doc/1.0.1/lifted-embedding.html#inserting

Since you do not accept Null in the database, I would suggest to change this:

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

to this:

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

and check that the password is not null.

As per pedrofurla suggestion you can add the following to the Users object:

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

and make the line more readable:

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

See http://slick.typesafe.com/doc/1.0.1/lifted-embedding.html#inserting

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top