Slick,H2插入查询自动增量ID

时间:2014-06-13 10:58:30

标签: scala h2 slick anorm slick-2.0

我在H2中有这张表:

CREATE TABLE computer (id BIGINT NOT NULL, name VARCHAR(255) NOT NULL, introduced TIMESTAMP, discontinued TIMESTAMP, company_id BIGINT, CONSTRAINT pk_computer PRIMARY KEY (id));
CREATE SEQUENCE computer_seq START WITH 1000;

为它自动生成类:

  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))
但是,我似乎无法插入新行。

下面的尝试
val row = ("name", new Timestamp((new Date).getTime), new Timestamp((new Date).getTime), 123)

DB.withDynSession {
    Computer.map( r =>
        (r.name, r.introduced, r.discontinued, r.companyId)
    ) += row
}

引发错误

[JdbcSQLException: NULL not allowed for column "ID"; SQL statement: INSERT INTO "COMPUTER" ("NAME","INTRODUCED","DISCONTINUED","COMPANY_ID") VALUES (?,?,?,?) [23502-175]]

同样的方法适用于MySQL和PostgreSQL,所以我猜测H2没有相同的主ID自动增量功能?如何使我的插入工作与光滑呢?

这是与Anorm相同的表的一个工作示例:

DB.withConnection { implicit connection =>
    SQL(
        """
            insert into computer values (
                (select next value for computer_seq), 
                {name}, {introduced}, {discontinued}, {company_id}
            )
        """
    ).on(
        'name -> "name",
        'introduced -> new Timestamp((new Date).getTime),
        'discontinued -> new Timestamp((new Date).getTime),
        'company_id -> 123
    ).executeUpdate()
}

2 个答案:

答案 0 :(得分:1)

据我所知,这不是一个Slick问题,但你的DDL语句并没有为H2指定自动增量。检查示例项目的play-slick SQL代码:https://github.com/playframework/play-slick/blob/master/samples/computer-database/conf/evolutions/default/1.sql

另请查看H2文档:http://www.h2database.com/html/grammar.html#column_definition

答案 1 :(得分:0)

尝试为case类提供id的默认值,如下所示:

case class ComputerRow(id: Long = 0, name: String, introduced: Option[java.sql.Timestamp], discontinued: Option[java.sql.Timestamp], companyId: Option[Long])

我不确定H2但是当我使用Postgres时,我通常会指定id字段的默认值以符合Slick的最终检查格式然后在数据库端,序列自动处理值插入。

编辑:

我可能错了,但我注意到你创建了一个序列而没有分配它:

CREATE TABLE computer (id BIGINT NOT NULL, name VARCHAR(255) NOT NULL, introduced TIMESTAMP, discontinued TIMESTAMP, company_id BIGINT, CONSTRAINT pk_computer PRIMARY KEY (id));
CREATE SEQUENCE computer_seq START WITH 1000;

Postgres中创建序列然后分配它们像这样:

create table users (
  id  bigint not null
);

create sequence users_seq;
alter table users alter column id set default nextval('users_seq');
H2中的

,据我所知,这就是你如何分配一个自动增量:

create table test(id bigint auto_increment, name varchar(255));

采取this SO question形式。