【发布时间】:2017-05-27 11:47:57
【问题描述】:
我有一个简单的 scala 和 play 代码,用于将产品插入数据库。
我在 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)
}
我使用 Option auto increment 字段更新了模型和 dao,但没有帮助。
【问题讨论】:
-
您是否了解了 Evolutions 如何与 Play 协同工作? playframework.com/documentation/2.5.x/Evolutions
-
是的,但是我的代码是使用一些未使用进化的教程创建的。你能解释一下我应该在 build.sbt 中添加什么而不是单独“进化”来触发查询吗?
-
在您的问题中,您说“甚至没有在数据库中创建表。”。如果没有 Evolutions,将不会为您创建该表。您需要使用 SQL 语句在 postgres 数据库中手动创建表。确保它对应于您的 ProductTableDef。
-
我在数据库中手动创建了“产品”表,但仍然没有添加数据。
-
如果您需要有关 play+slick 的教程,请查看我几个月前编写的(针对 play2.4.x)的教程:pedrorijo.com/blog/play-slick 代码可在 Github 上找到:@ 987654323@ 甚至还有一个 play2.5.x 分支:github.com/pedrorijo91/play-slick3-steps/tree/play2.5 希望对您有所帮助
标签: database postgresql scala playframework slick