提交 86af8d31 authored 作者: noelgrandin's avatar noelgrandin

Fix for a bug where we would sometimes use the wrong unique constraint to…

Fix for a bug where we would sometimes use the wrong unique constraint to validate foreign key constraints.
上级 5e36a786
......@@ -20,6 +20,7 @@ Change Log
<h2>Next Version (unreleased)</h2>
<ul><li>PostgreSQL compatibility: support for EXTRACT(WEEK FROM dateColumn).
Thanks to Prashant Bhat for the patch!
<ul><li>Fix for a bug where we would sometimes use the wrong unique constraint to validate foreign key constraints.
</li></ul>
<h2>Version 1.3.169 (2012-09-09)</h2>
......
......@@ -207,7 +207,7 @@ public class AlterTableAddConstraint extends SchemaCommand {
refIndex = null;
}
if (refIndex == null) {
refIndex = getUniqueIndex(refTable, refIndexColumns);
refIndex = getIndex(refTable, refIndexColumns);
if (refIndex == null) {
refIndex = createIndex(refTable, refIndexColumns, true);
isRefOwner = true;
......
......@@ -47,6 +47,7 @@ public class TestTriggersConstraints extends TestBase implements Trigger {
testTriggerAlterTable();
testTriggers();
testConstraints();
testMultiPartForeignKeys();
deleteDb("trigger");
}
......@@ -366,6 +367,37 @@ public class TestTriggersConstraints extends TestBase implements Trigger {
conn.close();
}
/**
* Regression test: we had a bug where the AlterTableAddConstraint class used to sometimes pick the wrong
* unique index for a foreign key.
*/
private void testMultiPartForeignKeys() throws SQLException {
Connection conn = getConnection("trigger");
Statement stat = conn.createStatement();
stat.execute("DROP TABLE IF EXISTS TEST1");
stat.execute("DROP TABLE IF EXISTS TEST2");
stat.execute("create table test1(id int primary key, col1 int)");
stat.execute("alter table test1 add constraint unq_test1 " +
"unique (id,col1)");
stat.execute("create table test2(id int primary key, col1 int)");
stat.execute("alter table test2 add constraint fk_test2 " +
"foreign key(id,col1) references test1 (id,col1)");
stat.execute("insert into test1 values (1,1)");
stat.execute("insert into test1 values (2,2)");
stat.execute("insert into test1 values (3,3)");
stat.execute("insert into test2 values (1,1)");
assertThrows(23506, stat).execute("insert into test2 values (2,1)");
assertSingleValue(stat, "select count(*) from test1", 3);
assertSingleValue(stat, "select count(*) from test2", 1);
stat.execute("drop table test1");
stat.execute("drop table test2");
conn.close();
}
private void testTriggers() throws SQLException {
mustNotCallTrigger = false;
Connection conn = getConnection("trigger");
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论