我有一個簡單的scala和播放代碼以將產品插入到數據庫中。Scala,play - 無法插入到Postgresql數據庫中
在application.conf我的數據庫配置是這樣的:
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"
表的定義和CRUD操作:
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)
}
}
服務:
object ProductService {
def addProduct(product: Product): Future[Int] = {
Products.add(product)
}
}
和控制器:
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)
}
一切看起來不錯,在過程中沒有錯誤(我使用郵遞員,創建json並將其發送到服務器)。但畢竟數據庫中沒有數據。即使表格不是在數據庫中創建的。我真的不知道爲什麼這不能添加到數據庫。
編輯:
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);
這是我使用手動創建表的腳本,然後我儘量節省數據FRM請求到數據庫中。從請求一切都很好讀(對象產品創建),但沒有數據仍然是安全的數據庫。
編輯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)
}
我更新模型和期權自動遞增場道,但它並沒有幫助。
您是否閱讀過Evolution如何與Play一起使用? https://www.playframework.com/documentation/2.5.x/Evolutions – sheunis
是的,但我的代碼創建了一些教程,沒有使用演變。你能解釋我應該添加什麼,而不是單獨的「進化」到build.sbt中來觸發查詢? – allocer
在你的問題中你陳述了「偶數表不是在數據庫中創建的」。如果沒有Evolution,表格將不會爲您創建。您需要使用SQL語句在postgres數據庫中手動創建表。確保它對應於您的ProductTableDef。 – sheunis