Skip to content

Commit 45ed6e1

Browse files
committed
Increase test coverage in RI_FKey_fk_upd_check_required()
This checks the code path of FKCONSTR_MATCH_FULL and RI_KEYS_SOME_NULL. Reviewed-by: Alvaro Herrera <alvherre@2ndquadrant.com> Reviewed-by: Mi Tar <mmitar@gmail.com> Discussion: https://www.postgresql.org/message-id/flat/7ae17c95-0c99-d420-032a-c271f510112b@2ndquadrant.com/
1 parent cdaf4a4 commit 45ed6e1

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

src/test/regress/expected/foreign_key.out

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,12 @@ SELECT * FROM FKTABLE;
143143
| | 8
144144
(5 rows)
145145

146+
-- Check update with part of key null
147+
UPDATE FKTABLE SET ftest1 = NULL WHERE ftest1 = 1;
148+
ERROR: insert or update on table "fktable" violates foreign key constraint "constrname"
149+
DETAIL: MATCH FULL does not allow mixing of null and nonnull key values.
150+
-- Check update with old and new key values equal
151+
UPDATE FKTABLE SET ftest1 = 1 WHERE ftest1 = 1;
146152
-- Try altering the column type where foreign keys are involved
147153
ALTER TABLE PKTABLE ALTER COLUMN ptest1 TYPE bigint;
148154
ALTER TABLE FKTABLE ALTER COLUMN ftest1 TYPE bigint;
@@ -158,11 +164,11 @@ SELECT * FROM PKTABLE;
158164
SELECT * FROM FKTABLE;
159165
ftest1 | ftest2 | ftest3
160166
--------+--------+--------
161-
1 | 3 | 5
162167
3 | 6 | 12
163168
| | 0
164169
| | 4
165170
| | 8
171+
1 | 3 | 5
166172
(5 rows)
167173

168174
DROP TABLE PKTABLE CASCADE;
@@ -1597,6 +1603,15 @@ INSERT INTO fk_notpartitioned_pk VALUES (2502, 2503);
15971603
INSERT INTO fk_partitioned_fk_3 (a, b) VALUES (2502, 2503);
15981604
-- this always works
15991605
INSERT INTO fk_partitioned_fk (a,b) VALUES (NULL, NULL);
1606+
-- MATCH FULL
1607+
INSERT INTO fk_notpartitioned_pk VALUES (1, 2);
1608+
CREATE TABLE fk_partitioned_fk_full (x int, y int) PARTITION BY RANGE (x);
1609+
CREATE TABLE fk_partitioned_fk_full_1 PARTITION OF fk_partitioned_fk_full DEFAULT;
1610+
ALTER TABLE fk_partitioned_fk_full ADD FOREIGN KEY (x, y) REFERENCES fk_notpartitioned_pk MATCH FULL;
1611+
INSERT INTO fk_partitioned_fk_full VALUES (1, NULL); -- fails
1612+
ERROR: insert or update on table "fk_partitioned_fk_full_1" violates foreign key constraint "fk_partitioned_fk_full_x_fkey"
1613+
DETAIL: MATCH FULL does not allow mixing of null and nonnull key values.
1614+
DROP TABLE fk_partitioned_fk_full;
16001615
-- ON UPDATE SET NULL
16011616
SELECT tableoid::regclass, a, b FROM fk_partitioned_fk WHERE b IS NULL ORDER BY a;
16021617
tableoid | a | b

src/test/regress/sql/foreign_key.sql

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,12 @@ UPDATE PKTABLE SET ptest1=1 WHERE ptest1=2;
9797
-- Check FKTABLE for update of matched row
9898
SELECT * FROM FKTABLE;
9999

100+
-- Check update with part of key null
101+
UPDATE FKTABLE SET ftest1 = NULL WHERE ftest1 = 1;
102+
103+
-- Check update with old and new key values equal
104+
UPDATE FKTABLE SET ftest1 = 1 WHERE ftest1 = 1;
105+
100106
-- Try altering the column type where foreign keys are involved
101107
ALTER TABLE PKTABLE ALTER COLUMN ptest1 TYPE bigint;
102108
ALTER TABLE FKTABLE ALTER COLUMN ftest1 TYPE bigint;
@@ -1204,6 +1210,14 @@ INSERT INTO fk_partitioned_fk_3 (a, b) VALUES (2502, 2503);
12041210
-- this always works
12051211
INSERT INTO fk_partitioned_fk (a,b) VALUES (NULL, NULL);
12061212

1213+
-- MATCH FULL
1214+
INSERT INTO fk_notpartitioned_pk VALUES (1, 2);
1215+
CREATE TABLE fk_partitioned_fk_full (x int, y int) PARTITION BY RANGE (x);
1216+
CREATE TABLE fk_partitioned_fk_full_1 PARTITION OF fk_partitioned_fk_full DEFAULT;
1217+
ALTER TABLE fk_partitioned_fk_full ADD FOREIGN KEY (x, y) REFERENCES fk_notpartitioned_pk MATCH FULL;
1218+
INSERT INTO fk_partitioned_fk_full VALUES (1, NULL); -- fails
1219+
DROP TABLE fk_partitioned_fk_full;
1220+
12071221
-- ON UPDATE SET NULL
12081222
SELECT tableoid::regclass, a, b FROM fk_partitioned_fk WHERE b IS NULL ORDER BY a;
12091223
UPDATE fk_notpartitioned_pk SET a = a + 1 WHERE a = 2502;

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy