I am using Play 2.0.2 with ScalaQuery 0.9.5.
I have the following simple model code:
case class Task(id: Long, name: String)
object Task extends Table[(Long, String)]("task") {
lazy val database = Database.forDataSource(DB.getDataSource())
def id = column[Long]("id", O PrimaryKey, O AutoInc)
def name = column[String]("name", O NotNull)
def create(task: Task) = database.withSession {
implicit db: Session => {
Task.name insert(task.name)
}
}
And the following code for handling Form submission:
val taskForm: Form[Task] = Form(
mapping(
"name" -> nonEmptyText
) {
(name) => Task(-1L, name)
} {
task => Some(task.name)
}
)
def newTask = Action {
implicit request =>
taskForm.bindFromRequest.fold(
errors => BadRequest(views.html.index(Task.all, errors)),
task => {
Task.create(task)
Redirect(routes.Application.tasks())
}
)
}
A few questions:
1) Is there a better method for handling transient primary key values than passing in a constant? Something similar to Anorm's NotAssigned?
2) Would it be better to add "id" -> ignored(-1L) to the Form mapping and use the Task's extractor functions?
3) Should the case class rather be defined without an id field?