[tarantool-patches] [PATCH 2/4] Fix creation of FK constraint in case of no child's PK
Nikita Pettik
korablev at tarantool.org
Fri Mar 29 21:24:22 MSK 2019
on_replace_dd_fk_constraint() assumes that child's primary index
exists: it is used to verify emptiness of space invoking index_size().
This commit fixes this obvious bug which could lead to crash.
---
src/box/alter.cc | 2 +-
test/sql/foreign-keys.result | 25 +++++++++++++++++++++++++
test/sql/foreign-keys.test.lua | 10 ++++++++++
3 files changed, 36 insertions(+), 1 deletion(-)
diff --git a/src/box/alter.cc b/src/box/alter.cc
index 40b32eafe..2f783c8e7 100644
--- a/src/box/alter.cc
+++ b/src/box/alter.cc
@@ -3901,7 +3901,7 @@ on_replace_dd_fk_constraint(struct trigger * /* trigger*/, void *event)
* checks on existing data in space.
*/
struct index *pk = space_index(child_space, 0);
- if (index_size(pk) > 0) {
+ if (pk != NULL && index_size(pk) > 0) {
tnt_raise(ClientError, ER_CREATE_FK_CONSTRAINT,
fk_def->name,
"referencing space must be empty");
diff --git a/test/sql/foreign-keys.result b/test/sql/foreign-keys.result
index 731807227..97c3e2442 100644
--- a/test/sql/foreign-keys.result
+++ b/test/sql/foreign-keys.result
@@ -385,5 +385,30 @@ box.sql.execute("CREATE TABLE t4 (id INT PRIMARY KEY REFERENCES t4);")
box.space.T4:drop()
---
...
+-- Make sure that child space can feature no PK.
+--
+t1 = box.schema.create_space("T1")
+---
+...
+box.space.T1:format({'ID'})
+---
+...
+t2 = box.schema.create_space("T2")
+---
+...
+i1 = box.space.T2:create_index('I1')
+---
+...
+box.sql.execute("ALTER TABLE t1 ADD CONSTRAINT fk FOREIGN KEY (id) REFERENCES t2;")
+---
+- error: 'Failed to create foreign key constraint ''FK'': foreign key refers to nonexistent
+ field'
+...
+t1:drop()
+---
+...
+t2:drop()
+---
+...
--- Clean-up SQL DD hash.
-test_run:cmd('restart server default with cleanup=1')
diff --git a/test/sql/foreign-keys.test.lua b/test/sql/foreign-keys.test.lua
index 6f4941e09..078c10c54 100644
--- a/test/sql/foreign-keys.test.lua
+++ b/test/sql/foreign-keys.test.lua
@@ -174,5 +174,15 @@ box.space.T1:drop()
box.sql.execute("CREATE TABLE t4 (id INT PRIMARY KEY REFERENCES t4);")
box.space.T4:drop()
+-- Make sure that child space can feature no PK.
+--
+t1 = box.schema.create_space("T1")
+box.space.T1:format({'ID'})
+t2 = box.schema.create_space("T2")
+i1 = box.space.T2:create_index('I1')
+box.sql.execute("ALTER TABLE t1 ADD CONSTRAINT fk FOREIGN KEY (id) REFERENCES t2;")
+t1:drop()
+t2:drop()
+
--- Clean-up SQL DD hash.
-test_run:cmd('restart server default with cleanup=1')
--
2.15.1
More information about the Tarantool-patches
mailing list