0

我正在使用老式的 ddlutils lib 在 hsql 中创建模式,并看到此错误。这很奇怪,因为主键只创建一次。

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
4

1 回答 1

2

IDENTITY 的简单使用意味着生成了一个主键。最好使用这种 SQL 标准形式:

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

从版本 2.3.4 开始支持导致错误的语法

于 2016-04-04T10:09:55.180 回答