javahsqldbddlutils

HSQL Error: org.hsqldb.HsqlException: primary key already exist


I am using the old-school ddlutils lib to create a schema in hsql, and am seeing this error. It is strange because the primary key is only created once.

org.apache.ddlutils.DatabaseOperationException: Error while executing SQL -- -----------------------------------------------------------------------
-- musician
-- -----------------------------------------------------------------------

CREATE TABLE musician
(
    id INTEGER IDENTITY,
    name VARCHAR(2147483647),
    PRIMARY KEY (id)
)
        at org.apache.ddlutils.platform.PlatformImplBase.evaluateBatch(PlatformImplBase.java:331)
        at org.apache.ddlutils.platform.PlatformImplBase.createTables(PlatformImplBase.java:424)
        at org.apache.ddlutils.platform.PlatformImplBase.createTables(PlatformImplBase.java:409)
        at my.data.SchemaSync.applySchema(SchemaSync.java:104)
        at my.data.SchemaSync.start(SchemaSync.java:42)
        at my.deploy.BaseEnvironment$1.run(BaseEnvironment.java:38)
        at java.lang.Thread.run(Thread.java:745)
Caused by: java.sql.SQLSyntaxErrorException: primary key already exist
        at org.hsqldb.jdbc.JDBCUtil.sqlException(Unknown Source)
        at org.hsqldb.jdbc.JDBCUtil.sqlException(Unknown Source)
        at org.hsqldb.jdbc.JDBCStatement.fetchResult(Unknown Source)
        at org.hsqldb.jdbc.JDBCStatement.executeUpdate(Unknown Source)
        at org.apache.ddlutils.platform.PlatformImplBase.evaluateBatch(PlatformImplBase.java:309)
        ... 6 more
Caused by: org.hsqldb.HsqlException: primary key already exist
        at org.hsqldb.error.Error.error(Unknown Source)
        at org.hsqldb.error.Error.error(Unknown Source)
        at org.hsqldb.ParserTable.readConstraint(Unknown Source)
        at org.hsqldb.ParserTable.readTableContentsSource(Unknown Source)
        at org.hsqldb.ParserTable.compileCreateTableBody(Unknown Source)
        at org.hsqldb.ParserTable.compileCreateTable(Unknown Source)
        at org.hsqldb.ParserDDL.compileCreate(Unknown Source)
        at org.hsqldb.ParserCommand.compilePart(Unknown Source)
        at org.hsqldb.ParserCommand.compileStatements(Unknown Source)
        at org.hsqldb.Session.executeDirectStatement(Unknown Source)
        at org.hsqldb.Session.execute(Unknown Source)
        ... 9 more

Solution

  • The simple use of IDENTITY implies a primary key is generated. It is better to use this SQL Standard form:

    CREATE TABLE musician
    (
        id INTEGER GENERATED BY DEFAULT AS IDENTITY,
        name VARCHAR(2147483647),
        PRIMARY KEY (id)
    )
    

    The syntax that caused the error is supported from version 2.3.4