Scala&;玩&;斯里克和;PostgreSQL自动增量

Scala&;玩&;斯里克和;PostgreSQL自动增量,postgresql,scala,playframework,playframework-2.1,slick,Postgresql,Scala,Playframework,Playframework 2.1,Slick,我在Scala中有以下代码: case class Product(id: Option[Long] = None, name: String, price: BigDecimal, description: String) object Products extends Table[Product]("product") { def id = column[Long]("id", O.AutoInc, O.PrimaryKey) def name = column[String]("

我在Scala中有以下代码:

case class Product(id: Option[Long] = None, name: String, price: BigDecimal, description: String)

object Products extends Table[Product]("product") {
  def id = column[Long]("id", O.AutoInc, O.PrimaryKey)
  def name = column[String]("name", O.NotNull)
  def price = column[BigDecimal]("price", O.NotNull)
  def description = column[String]("description", O.NotNull)

  def * = id.? ~ name ~ price ~ description <>(Product.apply _, Product.unapply _)

  def autoInc = * returning id

  def add(product: Product)(implicit s:Session): Long = {
    Products.autoInc.insert(product)
  }

  def all(implicit s:Session): List[Product] = {
    Query(Products).list
  }
}
我经常从PostgreSQL收到一条错误消息,说id不能为null。我完全同意这一点,但是为什么autoInc不设置id列呢?不是这样吗

我用游戏!2.1.2、Scala 2.10.0、PostgreSQL 9.3和play slick 0.3.3


提前感谢。

这里有一个建议,重写您的
autoInc
,并添加如下方法:

def autoInc = name ~ price ~ description returning id

def add(product: Product)(implicit s:Session): Long = {
    Products.autoInc.insert(p.name, p.price, p.description)
}

某些数据库不允许在自动增量列中插入null。也许是Postgres的情况。

你的模式是什么?它是用滑头还是手工制作的?很可能,id列需要是serial或bigserial类型,这实际上使它成为int或bigint类型,但也将默认值设置为nextval('product_id_seq'),并自动创建该序列。它是由Slick自动创建的:
create table“product”(“id”serial NOT NULL主键,“name”VARCHAR(254)NOT NULL,“price”十进制(21,2)不为空,“描述”VARCHAR(254)不为空。我认为它已经是串行的,但代码不起作用:(这是正确的:)(play slick计算机数据库示例错误)。示例不应该是
add(p:Product)…
.autoInc.insert(Product.name)…
?只有当您有autoInc=name并且只想插入产品名称时,将其他内容保留为默认值。
def autoInc = name ~ price ~ description returning id

def add(product: Product)(implicit s:Session): Long = {
    Products.autoInc.insert(p.name, p.price, p.description)
}