I have a simple scala and play code for inserting product into database.
My database config in application.conf looks like:
db.default.hikaricp.connectionTestQuery = "SELECT 1"
db.default.driver=org.postgresql.Driver
db.default.url="jdbc:postgresql://localhost:5432/shop"
db.default.user="postgres"
db.default.password="root"
Table definition and crud operations:
case class Product(id: Long, name: String, description: String, price: BigDecimal, amount: Int)
case class ProductFormData(name: String, description: String, price: BigDecimal, amount: Int)
object ProductForm {
val form = Form(
mapping(
"name" -> nonEmptyText,
"description" -> nonEmptyText,
"price" -> bigDecimal,
"amount" -> number
)(ProductFormData.apply)(ProductFormData.unapply)
)
}
class ProductTableDef(tag: Tag) extends Table[Product](tag, "product") {
def id = column[Long]("id", O.PrimaryKey, O.AutoInc)
def name = column[String]("name")
def description = column[String]("description")
def price = column[BigDecimal]("price")
def amount = column[Int]("amount")
override def * =
(id, name, description, price, amount) <> (Product.tupled, Product.unapply)
}
object Products {
val products = TableQuery[ProductTableDef]
private def db: Database = Database.forDataSource(DB.getDataSource())
def add(product: Product): Future[Int] = {
try db.run(products += product)
finally db.close
}
def delete(id: Long): Future[Int] = {
db.run(products.filter(_.id === id).delete)
}
def get(id: Long): Future[Option[Product]] = {
db.run(products.filter(_.id === id).result.headOption)
}
def listAll: Future[Seq[Product]] = {
db.run(products.result)
}
}
service:
object ProductService {
def addProduct(product: Product): Future[Int] = {
Products.add(product)
}
}
and controller:
def create() = Action(parse.json) { request =>
val name = (request.body \ "name").as[String]
val description = (request.body \ "description").as[String]
val price = (request.body \ "price").as[BigDecimal]
val amount = (request.body \ "amount").as[Int]
val product = Product(0, name, description, price, amount)
ProductService.addProduct(product)
Ok("name : " + product.name)
}
Everything looks good, no errors in process (I use postman, creating json and send it to server). But after all there is no data in databse. Even table is not created in database. I really don't know why this cannot be add to database.
EDIT:
create table "Product" ("id" BIGSERIAL NOT NULL PRIMARY KEY,"name" VARCHAR(254) NOT NULL,"description" VARCHAR(254) NOT NULL,"price" Decimal, "amount" BIGINT NOT NULL);
This is a script which I use to create table manually, then I try to save data frm request into database. From request everything is read fine (object Product is created) but no data still is safe into database.
EDIT 2:
case class Product(id: Option[Long], name: String, description: String, price: BigDecimal, amount: Int)
class ProductTableDef(tag: Tag) extends Table[Product](tag, "product") {
def id = column[Long]("id", O.PrimaryKey, O.AutoInc)
def name = column[String]("name")
def description = column[String]("description")
def price = column[BigDecimal]("price")
def amount = column[Int]("amount")
override def * =
(id.?, name, description, price, amount) <> (Product.tupled, Product.unapply)
}
I updated models and dao with Option auto increment field, but It didn't help.