提交 b6db8379 authored 作者: Thomas Mueller's avatar Thomas Mueller

ALTER TABLE could throw an exception "object already exists" in some cases.

上级 ee2e763d
...@@ -11,6 +11,7 @@ import java.sql.SQLException; ...@@ -11,6 +11,7 @@ import java.sql.SQLException;
import org.h2.command.Parser; import org.h2.command.Parser;
import org.h2.command.Prepared; import org.h2.command.Prepared;
import org.h2.constant.ErrorCode; import org.h2.constant.ErrorCode;
import org.h2.constraint.Constraint;
import org.h2.constraint.ConstraintReferential; import org.h2.constraint.ConstraintReferential;
import org.h2.engine.Database; import org.h2.engine.Database;
import org.h2.engine.DbObject; import org.h2.engine.DbObject;
...@@ -326,7 +327,17 @@ public class AlterTableAlterColumn extends SchemaCommand { ...@@ -326,7 +327,17 @@ public class AlterTableAlterColumn extends SchemaCommand {
} }
if (name.startsWith(tempName + "_")) { if (name.startsWith(tempName + "_")) {
name = name.substring(tempName.length() + 1); name = name.substring(tempName.length() + 1);
db.renameSchemaObject(session, (SchemaObject) child, name); SchemaObject so = (SchemaObject) child;
if (so instanceof Constraint) {
if (so.getSchema().findConstraint(session, name) != null) {
name = so.getSchema().getUniqueConstraintName(session, newTable);
}
} else if (so instanceof Index) {
if (so.getSchema().findIndex(session, name) != null) {
name = so.getSchema().getUniqueIndexName(session, newTable, name);
}
}
db.renameSchemaObject(session, so, name);
} }
} }
} }
......
...@@ -36,6 +36,7 @@ public class TestCases extends TestBase { ...@@ -36,6 +36,7 @@ public class TestCases extends TestBase {
} }
public void test() throws Exception { public void test() throws Exception {
testConstraintAlterTable();
testJoinWithView(); testJoinWithView();
testLobDecrypt(); testLobDecrypt();
testInvalidDatabaseName(); testInvalidDatabaseName();
...@@ -71,6 +72,18 @@ public class TestCases extends TestBase { ...@@ -71,6 +72,18 @@ public class TestCases extends TestBase {
deleteDb("cases"); deleteDb("cases");
} }
private void testConstraintAlterTable() throws SQLException {
deleteDb("cases");
Connection conn = getConnection("cases");
Statement stat = conn.createStatement();
stat.execute("create table parent (pid int)");
stat.execute("create table child (cid int primary key, pid int)");
stat.execute("alter table child add foreign key (pid) references parent(pid)");
stat.execute("alter table child add column c2 int");
stat.execute("alter table parent add column p2 varchar");
conn.close();
}
private void testEmptyBtreeIndex() throws SQLException { private void testEmptyBtreeIndex() throws SQLException {
deleteDb("cases"); deleteDb("cases");
Connection conn; Connection conn;
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论