如何通过 UCanAccess 创建一个 Table,其中两个外键引用指向另一个 Table?

How can I create a Table with two Foreign Key References to one other Table via UCanAccess?

直接在 MS-Access 中构建引用没有问题。 使用 UCanAccess 执行此操作会导致 "net.ucanaccess.jdbc.UcanaccessSQLException:...".

Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
Connection connection = DriverManager.getConnection("jdbc:ucanaccess://e:/TestDB.accdb;memory=true");
Statement statement = connection.createStatement();
//
String tableToBeReferenced = "PersonsTable";
String tableWithTheReferences = "RelationShipsTable";

try {// Tidy up
 statement.execute("DROP TABLE " + tableWithTheReferences);
} catch (Exception exeption) {}
try {// Tidy up
 statement.execute("DROP TABLE " + tableToBeReferenced);
} catch (Exception exeption) {}

statement.execute("CREATE TABLE " + tableToBeReferenced + "(ID autoincrement NOT NULL PRIMARY KEY,"//
    + "Name VARCHAR(255)"//
    + ")");

statement.execute("CREATE TABLE " + tableWithTheReferences + "(ID LONG NOT NULL PRIMARY KEY,"//
    + "RelationShip VARCHAR(255) NOT NULL DEFAULT 'FRIENDS',"//
    + "Person1Id LONG NOT NULL,"//
    + "Person2Id LONG NOT NULL)");

// reference #1
statement.execute("ALTER TABLE " + tableWithTheReferences + //
    " ADD CONSTRAINT FOREIGN_KEY_1 FOREIGN KEY (Person1Id) REFERENCES " //
    + tableToBeReferenced + "(ID) ON DELETE CASCADE");

// reference #2
statement.execute("ALTER TABLE " + tableWithTheReferences + //
    " ADD CONSTRAINT FOREIGN_KEY_2 FOREIGN KEY (Person2Id) REFERENCES " //
    + tableToBeReferenced + "(ID) ON DELETE CASCADE");

如果我只创建第一个引用,它就可以工作。 如果我只创建第二个引用,它就可以工作。

但是当我尝试构建两个引用时都失败了。

我认为它不会工作,因为你的两个外键都有 "ON DELETE CASCADE"。

我能够在 UCanAccess 4.0.3 下重现该问题。 HSQLDB 和 Jackcess 都没有在相同的两个表之间创建两个独立的 FK 关系的问题,因此看起来它可能是 UCanAccess 中的一个错误。我会将问题报告给 UCanAccess 开发团队,并根据任何消息更新此答案。

更新:

已针对此问题实施修复,并将包含在 UCanAccess 4.0.4 版本中。