删除约束后 h2 删除列失败

Posted

技术标签:

【中文标题】删除约束后 h2 删除列失败【英文标题】:h2 drop column after drop constraint failes 【发布时间】:2016-04-26 15:32:40 【问题描述】:

问题:

我们尝试在 h2 中运行您的更新脚本以进行测试。 对于每个版本,我们都有一个 update.sql 来在数据库上运行更新。

但我们在尝试删除约束(外键)时遇到问题:

错误:

Exception in thread "main" org.h2.jdbc.JdbcSQLException: Index "FP_ACL_PERMISSION_UNQ" belongs to constraint "CONSTRAINT_31B"; SQL statement:
ALTER TABLE FP_ACL_PERMISSION DROP INDEX FP_ACL_PERMISSION_UNQ; [90085-191]
    at org.h2.message.DbException.getJdbcSQLException(DbException.java:345)
    at org.h2.message.DbException.get(DbException.java:179)
    at org.h2.command.ddl.DropIndex.update(DropIndex.java:63)
    at org.h2.command.CommandContainer.update(CommandContainer.java:98)
    at org.h2.command.Command.executeUpdate(Command.java:258)
    at org.h2.jdbc.JdbcStatement.executeInternal(JdbcStatement.java:184)
    at org.h2.jdbc.JdbcStatement.execute(JdbcStatement.java:158)
    at com.rbs.mib.portal.HelloWorld.main(HelloWorld.java:63)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)

重现问题的代码:

public class ReproduceH2Problem

    public static void main(String... args) throws Exception 

        Class.forName("org.h2.Driver");
        Connection conn = DriverManager.getConnection("jdbc:h2:~/test;MODE=ORACLE", "sa", "sa");
        Statement stat = conn.createStatement();

        stat.execute("DROP ALL OBJECTS");

        // init v1.0.0
        stat.execute("CREATE TABLE FP_ACL" +
                "(" +
                "  ACL_ID         INTEGER NOT NULL," +
                "  REPORT_KEY     VARCHAR2(100)             NOT NULL," +
                "  PARENT_ACL_ID  INTEGER" +
                ");");

        stat.execute("CREATE TABLE FP_ACL_PERMISSION" +
                "(" +
                "  PERMISSION_ID  INTEGER NOT NULL," +
                "  ACL_ID         INTEGER                        NOT NULL," +
                "  NAME           VARCHAR2(100)" +
                ");");
        stat.execute("CREATE UNIQUE INDEX FP_ACL_PERMISSION_UNQ ON FP_ACL_PERMISSION" +
                "(ACL_ID, NAME);");

        stat.execute("ALTER TABLE FP_ACL_PERMISSION ADD " +
                "CHECK (ACL_ID IS NOT NULL);");

        stat.execute("ALTER TABLE FP_ACL_PERMISSION ADD " +
                "PRIMARY KEY " +
                "(PERMISSION_ID);");

        stat.execute("ALTER TABLE FP_ACL_PERMISSION ADD " +
                "CONSTRAINT FP_ACL_PERMISSION_UNQ " +
                "UNIQUE (ACL_ID, NAME);");


        // !! this seems to be the "bad guy" !!
        stat.execute("ALTER TABLE FP_ACL_PERMISSION ADD " +
                "FOREIGN KEY (ACL_ID) " +
                "REFERENCES FP_ACL (ACL_ID);");


        //update v1.0.1
        stat.execute("ALTER TABLE FP_ACL_PERMISSION DROP CONSTRAINT FP_ACL_PERMISSION_UNQ;");

        //here the problems start
        stat.execute("ALTER TABLE FP_ACL_PERMISSION DROP INDEX FP_ACL_PERMISSION_UNQ;");
        stat.execute("ALTER TABLE FP_ACL_PERMISSION DROP COLUMN ACL_ID;");
        stat.execute("ALTER TABLE FP_ACL_PERMISSION ADD CONSTRAINT FP_ACL_PERMISSION_UNQ UNIQUE (NAME);");

        stat.close();
        conn.close();
    

【问题讨论】:

H2 在添加外键时会创建“未命名”约束。 H2 还希望在删除索引之前先删除该约束!?!似乎我需要查询系统表以获取该约束名称,但这是非常解决方法'ish 重命名约束不起作用。 “不支持” github.com/h2database/h2database/issues/268 - 修复请求被拒绝为“按设计工作” 【参考方案1】:

我不得不通过在 sql 脚本中添加元命令来解决问题

无法重命名约束(h2 不支持) 直接子选择不起作用 alter table TABLE_NAME drop constraint (select unique_index_name from information_schema.constraints where table_name='TABLE_NAME' and CONSTRAINT_TYPE='REFERENTIAL' and COLUMN_LIST= ='SHORT_ID')

解决方法:

    元命令在 update.sql 脚本中:

    --H2-DROP-REFERENCE TABLE::FP_ACL_PERMISSION:: COLUMN::ACL_ID::
    ALTER TABLE FP_ACL_PERMISSION DROP INDEX FP_ACL_PERMISSION_UNQ;
    

    在 groovy 测试中过滤元命令:

    public static enum H2_META_COMMAND 
     DROP_REFERENCE("--H2-DROP-REFERENCE")
    
     private String command;
     H2_META_COMMAND(String command)
      this.command = command
     
    
     String getCommand()
       return command;
     
     
    

/**
 * This checks for custom H2 Meta commands to close oracle compatibility gap
 * @param sqlCommand a single command
 */
public static void executeH2MetaCommands(String sqlSingleCommand, Statement statement)
    if(sqlCommand.contains(H2_META_COMMAND.DROP_REFERENCE.getCommand()))
        int tableStart = sqlCommand.indexOf("TABLE::", sqlCommand.indexOf(H2_META_COMMAND.DROP_REFERENCE.getCommand()) + H2_META_COMMAND.DROP_REFERENCE.getCommand().size() ) + 7
        String table = sqlCommand.substring(tableStart, sqlCommand.indexOf("::",tableStart))
        int colStart = sqlCommand.indexOf("COLUMN::") + 8
        String col = sqlCommand.substring(colStart, sqlCommand.indexOf("::",colStart))
        ResultSet rs = statement.executeQuery("select CONSTRAINT_NAME from information_schema.constraints where table_name='"+ table+"' and CONSTRAINT_TYPE='REFERENTIAL' and COLUMN_LIST= '"+col+"'")
        rs.next()
        String constraintName = rs.getString(1)
        rs.close()
        statement.execute("ALTER TABLE FP_ACL_PERMISSION DROP constraint " + constraintName)
    

【讨论】:

以上是关于删除约束后 h2 删除列失败的主要内容,如果未能解决你的问题,请参考以下文章

无法删除表:外键约束失败

Laravel 关于删除完整性约束违规:1451 无法删除或更新父行:外键约束失败

如何解决这个问题 - 无法删除或更新父行:外键约束失败

传播级联删除引发外键约束失败

无法删除或更新父行:外键约束失败

spring boot:无法删除或更新父行:外键约束失败