Slick 3 insert not inserting but no error - scala

I'm trying to get a hang of Slick by doing a small test.
I'm trying to do an insert. The test runs, no errors, but when I check the db, no record has been inserted.
What am I doing wrong?
Here's my test code:
Note: I disabled the first 'flatMap' because when I wanted to test the second insert method, and that code was not executed when the first flatmap function was enabled.
Both insert methods do not insert a new record.
The first query for all items does work. The 'Test id:xx' lines are printed to console.
object TestSlick extends App {
import slick.driver.PostgresDriver.api._
import concurrent.ExecutionContext.Implicits.global
import concurrent.duration._
val config = ConfigFactory.load()
val username = config.getString("app.database.jdbc.username")
val password = config.getString("app.database.jdbc.password")
val url: String = config.getString("app.database.jdbc.url")
val db = Database.forURL(url, username, password)
try {
import Tables._
val res = db.run(headlines.result).map(_.foreach {
case HeadLineRow(id, _, _, _, _, companyId, text, from, days, end, user) =>
println(s"Test id:$id")
}).flatMap { _ =>
// println("Inserting....")
// val ts = Timestamp.valueOf(LocalDateTime.now())
// val insertAction: DBIO[Option[Int]] = (headlines returning headlines.map(_.id)) +=
// HeadLineRow(None, 100, 100, "tekst", ts, 5, ts, None, None, None, None)
//
// db.run(insertAction.transactionally.map(
// newId => println(s"New id: $newId"))
// )
// }.flatMap { _ =>
println("Inserting....(2)")
val ts = Timestamp.valueOf(LocalDateTime.now())
val insertAction = headlines.map(p => p) += HeadLineRow(None, 1921, 65, "tekst2", ts, 5, ts, None, None, None, None)
db.run(insertAction.transactionally.map(
r => println(s"Insert result: ${r}"))
)
}
Await.ready(res, 30 seconds);
} finally db.close()
}
And my table (generated using Slick's generator and then adjusted a bit (auto-inc id, swapped some properties around))
package com.wanneerwerkik.db.slick
// AUTO-GENERATED Slick data model
/** Stand-alone Slick data model for immediate use */
object Tables extends {
val profile = slick.driver.PostgresDriver
} with Tables
/** Slick data model trait for extension, choice of backend or usage in the cake pattern. (Make sure to initialize this late.) */
trait Tables {
val profile: slick.driver.JdbcProfile
import profile.api._
import slick.model.ForeignKeyAction
import slick.collection.heterogeneous._
import slick.collection.heterogeneous.syntax._
// NOTE: GetResult mappers for plain SQL are only generated for tables where Slick knows how to map the types of all columns.
import slick.jdbc.{GetResult => GR}
/** DDL for all tables. Call .create to execute. */
lazy val schema = Array(headlines.schema).reduceLeft(_ ++ _)
#deprecated("Use .schema instead of .ddl", "3.0")
def ddl = schema
/**
* Entity class storing rows of table 'head_line_bar'
* #param id Database column id SqlType(int4), PrimaryKey
* #param createdBy Database column created_by SqlType(int4), Default(None)
* #param createdOn Database column created_on SqlType(timestamp), Default(None)
* #param updatedBy Database column updated_by SqlType(int4), Default(None)
* #param updatedOn Database column updated_on SqlType(timestamp), Default(None)
* #param companyId Database column company_id SqlType(int4), Default(None)
* #param contentType Database column content_type SqlType(varchar), Length(255,true), Default(None)
* #param fromDate Database column from_date SqlType(timestamp), Default(None)
* #param numberofdays Database column numberofdays SqlType(int4), Default(None)
* #param uptoEndDate Database column upto_end_date SqlType(timestamp), Default(None)
* #param userId Database column user_id SqlType(int4), Default(None)
*/
case class HeadLineRow(
id: Option[Int],
userId: Int,
companyId: Int,
contentType: String,
fromDate: java.sql.Timestamp,
numberofdays: Int,
uptoEndDate: java.sql.Timestamp,
createdBy: Option[Int] = None,
createdOn: Option[java.sql.Timestamp] = None,
updatedBy: Option[Int] = None,
updatedOn: Option[java.sql.Timestamp] = None
)
/** GetResult implicit for fetching HeadLineBarRow objects using plain SQL queries */
implicit def GetResultHeadLineRow(implicit e0: GR[Int], e1: GR[Option[Int]], e2: GR[Option[java.sql.Timestamp]], e3: GR[Option[String]]): GR[HeadLineRow] = GR{
prs => import prs._
HeadLineRow.tupled((<<?[Int], <<[Int], <<[Int], <<[String], <<[java.sql.Timestamp], <<[Int], <<[java.sql.Timestamp], <<?[Int], <<?[java.sql.Timestamp], <<?[Int], <<?[java.sql.Timestamp]))
}
/**
* Table description of table head_line_bar.
* Objects of this class serve as prototypes for rows in queries.
*/
class Headlines(_tableTag: Tag) extends Table[HeadLineRow](_tableTag, "head_line_bar") {
def * = (id, userId, companyId, contentType, fromDate, numberofdays, uptoEndDate, createdBy, createdOn, updatedBy, updatedOn) <> (HeadLineRow.tupled, HeadLineRow.unapply)
/** Maps whole row to an option. Useful for outer joins. */
def ? = (Rep.Some(id), userId, companyId, contentType, fromDate, numberofdays, uptoEndDate, createdBy, createdOn, updatedBy, updatedOn).shaped.<>({r=>import r._; _1.map(_=> HeadLineRow.tupled((_1.get, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11)))}, (_:Any) => throw new Exception("Inserting into ? projection not supported."))
/** Database column id SqlType(int4), PrimaryKey */
val id: Rep[Option[Int]] = column[Option[Int]]("id", O.PrimaryKey, O.AutoInc)
/** Database column user_id SqlType(int4), Default(None) */
val userId: Rep[Int] = column[Int]("user_id")
/** Database column company_id SqlType(int4), Default(None) */
val companyId: Rep[Int] = column[Int]("company_id")
/** Database column content_type SqlType(varchar), Length(255,true), Default(None) */
val contentType: Rep[String] = column[String]("content_type", O.Length(255,varying=true))
/** Database column from_date SqlType(timestamp), Default(None) */
val fromDate: Rep[java.sql.Timestamp] = column[java.sql.Timestamp]("from_date")
/** Database column numberofdays SqlType(int4), Default(None) */
val numberofdays: Rep[Int] = column[Int]("numberofdays")
/** Database column upto_end_date SqlType(timestamp), Default(None) */
val uptoEndDate: Rep[java.sql.Timestamp] = column[java.sql.Timestamp]("upto_end_date")
/** Database column created_by SqlType(int4), Default(None) */
val createdBy: Rep[Option[Int]] = column[Option[Int]]("created_by", O.Default(None))
/** Database column created_on SqlType(timestamp), Default(None) */
val createdOn: Rep[Option[java.sql.Timestamp]] = column[Option[java.sql.Timestamp]]("created_on", O.Default(None))
/** Database column updated_by SqlType(int4), Default(None) */
val updatedBy: Rep[Option[Int]] = column[Option[Int]]("updated_by", O.Default(None))
/** Database column updated_on SqlType(timestamp), Default(None) */
val updatedOn: Rep[Option[java.sql.Timestamp]] = column[Option[java.sql.Timestamp]]("updated_on", O.Default(None))
}
/** Collection-like TableQuery object for table HeadLineBar */
lazy val headlines = new TableQuery(tag => new Headlines(tag))
}
Log output is too big to paste here so I put it in this gist.
As suggested I added a readLine to wait for the result, but it was already output the same stuff. I also added a completion handler on the Future to print it's Success or Failure. Apparently it fails with a RejectedExecutionException. Why?
Failure: java.util.concurrent.RejectedExecutionException: Task slick.backend.DatabaseComponent$DatabaseDef$$anon$2#2e4db0df rejected from java.util.concurrent.ThreadPoolExecutor#43760a50[Terminated, pool size = 0, active threads = 0, queued tasks = 0, completed tasks = 1]

This is just a guess, but maybe your testing framework is somehow confused by the fact that flatMap internally spawns a new task, requiring again the execution context (see e.g. this thread - it's about Scala 2.10 but I think this hasn't changed). So the resources are freed before your insert executes.
Have you tried putting a println in the finally block, to see if this is called before or after the message accompanying the insert?
Have you tried running both futures synchronously, using await? Likely you will not get this issue in this case.
You may consider testing with full asynchronous support, see e.g. this link.

Related

Slick: How to filter by isBefore Timestamp database attribute?

I naively did the following on Slick 4.0.1. I have an expiry database field and I need to find all the rows where the expiry is before a given timestamp.
This is what the Slick mapping looks like:
/** Table description of table auth_token. Objects of this class serve as prototypes for rows in queries. */
class AuthToken(_tableTag: Tag) extends profile.api.Table[AuthTokenRow](_tableTag, Some("myappdb"), "auth_token") with IdentifyableTable[Long] {
override def id = userId
def * = (userId, tokenId, expiry) <> (AuthTokenRow.tupled, AuthTokenRow.unapply)
/** Maps whole row to an option. Useful for outer joins. */
def ? = ((Rep.Some(userId), Rep.Some(tokenId), Rep.Some(expiry))).shaped.<>({ r => import r._; _1.map(_ => AuthTokenRow.tupled((_1.get, _2.get, _3.get))) }, (_: Any) => throw new Exception("Inserting into ? projection not supported."))
/** Database column user_id SqlType(BIGINT UNSIGNED) */
val userId: Rep[Long] = column[Long]("user_id")
/** Database column token_id SqlType(CHAR), Length(36,false) */
val tokenId: Rep[String] = column[String]("token_id", O.Length(36, varying = false))
/** Database column expiry SqlType(TIMESTAMP) */
val expiry: Rep[java.sql.Timestamp] = column[java.sql.Timestamp]("expiry")
/** Foreign key referencing User (database name auth_token_ibfk_1) */
lazy val userFk = foreignKey("auth_token_ibfk_1", userId, User)(r => r.id, onUpdate = ForeignKeyAction.NoAction, onDelete = ForeignKeyAction.Cascade)
/** Index over (tokenId) (database name idx_token_id) */
val index1 = index("idx_token_id", tokenId)
}
/** Collection-like TableQuery object for table AuthToken */
lazy val AuthToken = new TableQuery(tag => new AuthToken(tag))
and then I need to do:
/**
* Finds expired tokens.
*
* #param dateTime The current date time.
*/
def findExpired(dateTime: org.joda.time.DateTime): Future[Seq[AuthTokenRow]] = {
val action = AuthToken.filter(authToken => authToken.expiry.isBefore(dateTime)).result
db.run(action)
}
How to cover this use-case correctly/officially in Slick?
I solved this by reusing the project slick-joda-mapper. This solution is a bit involved because of working with the Slick generator etc but it is VERY clean, I love it. Basically it enables mapping database date time types to the Joda types like org.joda.time.DateTime. Therefore my solution becomes as simple as using comparison operators supported by Joda DateTime:
/**
* Finds expired tokens.
*
* #param dateTime The current date time.
*/
override def findExpired(dateTime: org.joda.time.DateTime): Future[Seq[AuthTokenRow]] = {
val action = AuthToken.filter(authToken => authToken.expiry < dateTime).result
db.run(action)
}

Slick insert into postgreSQL

I am just trying to add a row from slick to my postgreSQL Database.
Here what I am trying to do :
val dbConfig = DatabaseConfigProvider.get[JdbcProfile](Play.current)
import dbConfig.driver.api._
val query = Task += new TaskRow(5, "taskName", status = "other")
println(Task.insertStatement)
val resultingQuery = dbConfig.db.run(query).map(res => "Task successfully added").recover {
case ex: Exception => ex.getCause.getMessage
}
Here the result of println :
insert into "task" ("taskname","description","status","datetask","pediocitynumber","periodicitytype") values (?,?,?,?,?,?)
I don't have any result exception or success from the resulting query.
Code generate by slick-codegen 3.1.1 :
case class TaskRow(taskid: Int, taskname: String, description: Option[String] = None, status: String, datetask: Option[java.sql.Timestamp] = None, pediocitynumber: Option[Int] = None, periodicitytype: Option[String] = None)
/** GetResult implicit for fetching TaskRow objects using plain SQL queries */
implicit def GetResultTaskRow(implicit e0: GR[Int], e1: GR[String], e2: GR[Option[String]], e3: GR[Option[java.sql.Timestamp]], e4: GR[Option[Int]]): GR[TaskRow] = GR{
prs => import prs._
TaskRow.tupled((<<[Int], <<[String], <<?[String], <<[String], <<?[java.sql.Timestamp], <<?[Int], <<?[String]))
}
/** Table description of table task. Objects of this class serve as prototypes for rows in queries. */
class Task(_tableTag: Tag) extends Table[TaskRow](_tableTag, "task") {
def * = (taskid, taskname, description, status, datetask, pediocitynumber, periodicitytype) <> (TaskRow.tupled, TaskRow.unapply)
/** Maps whole row to an option. Useful for outer joins. */
def ? = (Rep.Some(taskid), Rep.Some(taskname), description, Rep.Some(status), datetask, pediocitynumber, periodicitytype).shaped.<>({r=>import r._; _1.map(_=> TaskRow.tupled((_1.get, _2.get, _3, _4.get, _5, _6, _7)))}, (_:Any) => throw new Exception("Inserting into ? projection not supported."))
/** Database column taskid SqlType(serial), AutoInc, PrimaryKey */
val taskid: Rep[Int] = column[Int]("taskid", O.AutoInc, O.PrimaryKey)
/** Database column taskname SqlType(text) */
val taskname: Rep[String] = column[String]("taskname")
/** Database column description SqlType(text), Default(None) */
val description: Rep[Option[String]] = column[Option[String]]("description", O.Default(None))
/** Database column status SqlType(statustask) */
val status: Rep[String] = column[String]("status")
/** Database column datetask SqlType(timestamp), Default(None) */
val datetask: Rep[Option[java.sql.Timestamp]] = column[Option[java.sql.Timestamp]]("datetask", O.Default(None))
/** Database column pediocitynumber SqlType(int4), Default(None) */
val pediocitynumber: Rep[Option[Int]] = column[Option[Int]]("pediocitynumber", O.Default(None))
/** Database column periodicitytype SqlType(periodicitytype), Default(None) */
val periodicitytype: Rep[Option[String]] = column[Option[String]]("periodicitytype", O.Default(None))
}
/** Collection-like TableQuery object for table Task */
lazy val Task = new TableQuery(tag => new Task(tag))
Could someone explain what I am doing wrong?
EDIT:
To clarify my question :
The row is not added to the table, it seems that nothing happen. I can't see any exception or error thrown.
I think it could come from the sql statement and the questions marks (values (?,?,?,?,?,?)). It should be the actual values of the fields, right?
More code:
class Application #Inject()(dbConfigProvider: DatabaseConfigProvider) extends Controller {
def index = Action {
Ok(views.html.main())
}
def taskSave = Action.async { implicit request =>
println(request.body.asJson)
val dbConfig = DatabaseConfigProvider.get[JdbcProfile](Play.current)
import dbConfig.driver.api._
val query = Task += new TaskRow(5, "taskName", status = "other")
println(Task.insertStatement)
println(query)
val resultingQuery = dbConfig.db.run(query).map(res => "TAsk successfully added").recover {
case ex: Exception => ex.getCause.getMessage
}
resultingQuery.map(r => println("result : " + r))
Future(Ok(""))
}
}
route :
PUT /task-save controllers.Application.taskSave
The statement is not wrong because of the values (?,?,? ...).
The lazy val Task is of type TableQuery[...] . When you call insertStatement on this object you get back a string that represents the template SQL string that runs in the background. That is expected.
So that is not the indicator of the problem.
Try something like this :
val db = Database.forConfig("h2mem1")
try {
Await.result(db.run(DBIO.seq(
// create the schema
Task.schema.create,
// insert two User instances
Task += (2, "abcd", "201-01-01"),
// print the users (select * from USERS)
Task.result.map(println))), Duration.Inf)
} finally db.close

Slick: dynamic sortBy in a query with left join

This is a problem derived from another question. I need to be able to dynamically pass a column to be sorted on in a Slick query which has a left join. The problem in this particular situation is that left joined table becomes optional and I have no idea how to handle that. If I make table Company not optional I'm getting SlickException: Read NULL value for ResultSet column Path
Example:
def list(filter: String, orderBy: Int) = {
DB.withDynSession {
val data = for {
(computer, company) <- Computer.where(_.name like filter) leftJoin
Company on (_.companyId === _.id)
} yield (computer, company.?)
val sortedData = orderBy match {
case 2 => data.sortBy(_._1.name) //Works ok, column from a primary table
case 3 => data.sortBy(_._2.name) //Error "Cannot resolve symbol name", because table is optional
}
}
}
Slick auto generated table classes used in example above:
package tables
// AUTO-GENERATED Slick data model
/** Stand-alone Slick data model for immediate use */
object Tables extends {
val profile = scala.slick.driver.H2Driver
} with Tables
/** Slick data model trait for extension, choice of backend or usage in the cake pattern. (Make sure to initialize this late.) */
trait Tables {
val profile: scala.slick.driver.JdbcProfile
import profile.simple._
import scala.slick.model.ForeignKeyAction
// NOTE: GetResult mappers for plain SQL are only generated for tables where Slick knows how to map the types of all columns.
import scala.slick.jdbc.{GetResult => GR}
/** DDL for all tables. Call .create to execute. */
lazy val ddl = Company.ddl ++ Computer.ddl
/** Entity class storing rows of table Company
* #param id Database column ID PrimaryKey
* #param name Database column NAME */
case class CompanyRow(id: Long, name: String)
/** GetResult implicit for fetching CompanyRow objects using plain SQL queries */
implicit def GetResultCompanyRow(implicit e0: GR[Long], e1: GR[String]): GR[CompanyRow] = GR{
prs => import prs._
CompanyRow.tupled((<<[Long], <<[String]))
}
/** Table description of table COMPANY. Objects of this class serve as prototypes for rows in queries. */
class Company(tag: Tag) extends Table[CompanyRow](tag, "COMPANY") {
def * = (id, name) <> (CompanyRow.tupled, CompanyRow.unapply)
/** Maps whole row to an option. Useful for outer joins. */
def ? = (id.?, name.?).shaped.<>({r=>import r._; _1.map(_=> CompanyRow.tupled((_1.get, _2.get)))}, (_:Any) => throw new Exception("Inserting into ? projection not supported."))
/** Database column ID PrimaryKey */
val id: Column[Long] = column[Long]("ID", O.PrimaryKey)
/** Database column NAME */
val name: Column[String] = column[String]("NAME")
}
/** Collection-like TableQuery object for table Company */
lazy val Company = new TableQuery(tag => new Company(tag))
/** Entity class storing rows of table Computer
* #param id Database column ID PrimaryKey
* #param name Database column NAME
* #param introduced Database column INTRODUCED
* #param discontinued Database column DISCONTINUED
* #param companyId Database column COMPANY_ID */
case class ComputerRow(id: Long, name: String, introduced: Option[java.sql.Timestamp], discontinued: Option[java.sql.Timestamp], companyId: Option[Long])
/** GetResult implicit for fetching ComputerRow objects using plain SQL queries */
implicit def GetResultComputerRow(implicit e0: GR[Long], e1: GR[String], e2: GR[Option[java.sql.Timestamp]], e3: GR[Option[Long]]): GR[ComputerRow] = GR{
prs => import prs._
ComputerRow.tupled((<<[Long], <<[String], <<?[java.sql.Timestamp], <<?[java.sql.Timestamp], <<?[Long]))
}
/** Table description of table COMPUTER. Objects of this class serve as prototypes for rows in queries. */
class Computer(tag: Tag) extends Table[ComputerRow](tag, "COMPUTER") {
def * = (id, name, introduced, discontinued, companyId) <> (ComputerRow.tupled, ComputerRow.unapply)
/** Maps whole row to an option. Useful for outer joins. */
def ? = (id.?, name.?, introduced, discontinued, companyId).shaped.<>({r=>import r._; _1.map(_=> ComputerRow.tupled((_1.get, _2.get, _3, _4, _5)))}, (_:Any) => throw new Exception("Inserting into ? projection not supported."))
/** Database column ID PrimaryKey */
val id: Column[Long] = column[Long]("ID", O.PrimaryKey)
/** Database column NAME */
val name: Column[String] = column[String]("NAME")
/** Database column INTRODUCED */
val introduced: Column[Option[java.sql.Timestamp]] = column[Option[java.sql.Timestamp]]("INTRODUCED")
/** Database column DISCONTINUED */
val discontinued: Column[Option[java.sql.Timestamp]] = column[Option[java.sql.Timestamp]]("DISCONTINUED")
/** Database column COMPANY_ID */
val companyId: Column[Option[Long]] = column[Option[Long]]("COMPANY_ID")
/** Foreign key referencing Company (database name FK_COMPUTER_COMPANY_1) */
lazy val companyFk = foreignKey("FK_COMPUTER_COMPANY_1", companyId, Company)(r => r.id, onUpdate=ForeignKeyAction.Restrict, onDelete=ForeignKeyAction.Restrict)
}
/** Collection-like TableQuery object for table Computer */
lazy val Computer = new TableQuery(tag => new Computer(tag))
}
.? is implemented using <> which prevents you from later accessing members. So you need to apply the sorting before you do the .?
val data = for {
(computer, company) <- Computer.where(_.name like filter) leftJoin
Company on (_.companyId === _.id)
} yield (computer, company) // <- no .?
val sortedData = orderBy match {
case 2 => data.sortBy(_._1.name) //Works ok, column from a primary table
case 3 => data.sortBy(_._2.name) //Error "Cannot resolve symbol name", because table is optional
}
val optionalJoinData = sortedData.map{
case (computer, company) => (computer, company.?)
} // <- do .? last

Slick 2.0.2 Error: value === is not a member of Tables.profile.simple.Column[String]

I am trying to start using slick. I have a h2-database and generated classes with scala.slick.model.codegen.SourceCodeGenerator. But when try I try following the examples and query my db using these classes I get scala-errors.
The generated code looks as follows:
/** Entity class storing rows of table User
* #param id Database column ID PrimaryKey
* #param firstname Database column FIRSTNAME
* #param lastname Database column LASTNAME */
case class UserRow(id: String, firstname: Option[String], lastname: Option[String])
/** GetResult implicit for fetching UserRow objects using plain SQL queries */
implicit def GetResultUserRow(implicit e0: GR[String], e1: GR[Option[String]]): GR[UserRow] = GR{
prs => import prs._
UserRow.tupled((<<[String], <<?[String], <<?[String]))
}
/** Table description of table USER. Objects of this class serve as prototypes for rows in queries. */
class User(tag: Tag) extends Table[UserRow](tag, "USER") {
def * = (id, firstname, lastname) <> (UserRow.tupled, UserRow.unapply)
/** Maps whole row to an option. Useful for outer joins. */
def ? = (id.?, firstname, lastname).shaped.<>({r=>import r._; _1.map(_=> UserRow.tupled((_1.get, _2, _3)))}, (_:Any) => throw new Exception("Inserting into ? projection not supported."))
/** Database column ID PrimaryKey */
val id: Column[String] = column[String]("ID", O.PrimaryKey)
/** Database column FIRSTNAME */
val firstname: Column[Option[String]] = column[Option[String]]("FIRSTNAME")
/** Database column LASTNAME */
val lastname: Column[Option[String]] = column[Option[String]]("LASTNAME")
}
/** Collection-like TableQuery object for table User */
lazy val User = new TableQuery(tag => new User(tag))
And this is my query:
val userResultList = for {
u <- User if u.id === "foo"
} yield u
which results in:
Error:(137, 29) value === is not a member of db.Tables.profile.simple.Column[String]
u <- User if u.id === user.id
^
What's wrong?
For slick 2.x, just import XXXDriver.simple._ and the compiler will be happy.
For slick 3.x, its XXXDriver.api._

How make simple slick insert with Lifted query and Option columns

I can't seem to understand how to make a simple insert into a Database using Slick. I'm using tables generator with my Oracle db and I'm getting something like this:
case class SimulatonRow(id: scala.math.BigDecimal, startDate: Option[java.sql.Timestamp], endDate: Option[java.sql.Timestamp], numberOfProc: Option[scala.math.BigDecimal], code: String)
/** GetResult implicit for fetching SimulatonRow objects using plain SQL queries */
implicit def GetResultSimulatonRow(implicit e0: GR[scala.math.BigDecimal], e1: GR[Option[java.sql.Timestamp]], e2: GR[Option[scala.math.BigDecimal]], e3: GR[String]): GR[SimulatonRow] = GR{
prs => import prs._
SimulatonRow.tupled((<<[scala.math.BigDecimal], <<?[java.sql.Timestamp], <<?[java.sql.Timestamp], <<?[scala.math.BigDecimal], <<[String]))
}
class Simulaton(tag: Tag) extends Table[SimulatonRow](tag, Some("BPRISK"), "SIMULATON") {
def * = (id, startDate, endDate, numberOfProc, code) <> (SimulatonRow.tupled, SimulatonRow.unapply)
/** Maps whole row to an option. Useful for outer joins. */
def ? = (id.?, startDate, endDate, numberOfProc, code.?).shaped.<>({r=>import r._; _1.map(_=> SimulatonRow.tupled((_1.get, _2, _3, _4, _5.get)))}, (_:Any) => throw new Exception("Inserting into ? projection not supported."))
/** Database column ID PrimaryKey */
val id: Column[scala.math.BigDecimal] = column[scala.math.BigDecimal]("ID", O.PrimaryKey, O.AutoInc)
/** Database column START_DATE */
val startDate: Column[Option[java.sql.Timestamp]] = column[Option[java.sql.Timestamp]]("START_DATE")
/** Database column END_DATE */
val endDate: Column[Option[java.sql.Timestamp]] = column[Option[java.sql.Timestamp]]("END_DATE")
/** Database column NUMBER_OF_PROC */
val numberOfProc: Column[Option[scala.math.BigDecimal]] = column[Option[scala.math.BigDecimal]]("NUMBER_OF_PROC")
/** Database column CODE */
val code: Column[String] = column[String]("CODE")
}
Then I add AutoInc option to the primary key.
I tried variations of 'insert' with TableQuery[Simulaton].map or InsertInvoker, but the code cannot be compiled.
How does it work?
I need to make an 'insert' with autoincremental PK and columns - code, startDate.
// import driver specific stuff
import MySQLDriver.simple._
// import generated code
import Tables._
// define db connection
val db = Database.for...
// create a connection
db.withSession{ implicit session =>
// select what should be inserted into
TableQuery[Simulaton].map(s => (s.code,s.startDate))
// insert instead or running the query
.insert( ("some code",Some(DateTime.now)) )
}