gpt4 book ai didi

database - h2 删除约束失败后删除列

转载 作者:搜寻专家 更新时间:2023-10-30 19:50:33 25 4
gpt4 key购买 nike

问题:

我们尝试在 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();
}
}

最佳答案

我不得不通过在 sql 脚本中添加元命令来做一个解决方法

  • 不能重命名约束(h2不支持)
  • 直接子选择不起作用改变表 TABLE_NAME 删除约束
    (选择 unique_index_name
    来自 information_schema.constraints
    其中 table_name='TABLE_NAME' and CONSTRAINT_TYPE='REFERENTIAL' and COLUMN_LIST= ='SHORT_ID')

解决方法:

  1. update.sql 脚本中的元命令:

    --H2-DROP-REFERENCE TABLE::FP_ACL_PERMISSION:: COLUMN::ACL_ID::
    ALTER TABLE FP_ACL_PERMISSION DROP INDEX FP_ACL_PERMISSION_UNQ;
  2. 在 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)
}
}

关于database - h2 删除约束失败后删除列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36869475/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com