You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Add tests for foreign keys with case-insensitive collations
Some of the behaviors of the different referential actions, such as
the difference between NO ACTION and RESTRICT are best illustrated
using a case-insensitive collation. So add some tests for that.
(What is actually being tested here is the behavior with values that
are "distinct" (binary different) but compare as equal. Another way
to do that would be with positive and negative zeroes with float
types. But this way seems nicer and more flexible.)
Discussion: https://www.postgresql.org/message-id/[email protected]
CREATE TABLE test11fk (x text COLLATE case_sensitive REFERENCES test11pk (x) ON UPDATE CASCADE ON DELETE CASCADE); -- error
2025
2025
ERROR: foreign key constraint "test11fk_x_fkey" cannot be implemented
2026
2026
DETAIL: Key columns "x" of the referencing table and "x" of the referenced table have incompatible collations: "case_sensitive" and "case_insensitive". If either collation is nondeterministic, then both collations have to be the same.
2027
+
-- foreign key actions
2028
+
-- Some of the behaviors are most easily visible with a
2029
+
-- case-insensitive collation.
2030
+
CREATE TABLE test12pk (x text COLLATE case_insensitive PRIMARY KEY);
2031
+
CREATE TABLE test12fk (a int, b text COLLATE case_insensitive REFERENCES test12pk (x) ON UPDATE NO ACTION);
2032
+
INSERT INTO test12pk VALUES ('abc');
2033
+
INSERT INTO test12fk VALUES (1, 'abc'), (2, 'ABC');
2034
+
UPDATE test12pk SET x = 'ABC' WHERE x = 'abc'; -- ok
2035
+
SELECT * FROM test12pk;
2036
+
x
2037
+
-----
2038
+
ABC
2039
+
(1 row)
2040
+
2041
+
SELECT * FROM test12fk; -- no updates here
2042
+
a | b
2043
+
---+-----
2044
+
1 | abc
2045
+
2 | ABC
2046
+
(2 rows)
2047
+
2048
+
DROP TABLE test12pk, test12fk;
2049
+
CREATE TABLE test12pk (x text COLLATE case_insensitive PRIMARY KEY);
2050
+
CREATE TABLE test12fk (a int, b text COLLATE case_insensitive REFERENCES test12pk (x) ON UPDATE RESTRICT);
2051
+
INSERT INTO test12pk VALUES ('abc');
2052
+
INSERT INTO test12fk VALUES (1, 'abc'), (2, 'ABC');
2053
+
UPDATE test12pk SET x = 'ABC' WHERE x = 'abc'; -- restrict violation
2054
+
ERROR: update or delete on table "test12pk" violates foreign key constraint "test12fk_b_fkey" on table "test12fk"
2055
+
DETAIL: Key (x)=(abc) is still referenced from table "test12fk".
2056
+
SELECT * FROM test12pk;
2057
+
x
2058
+
-----
2059
+
abc
2060
+
(1 row)
2061
+
2062
+
SELECT * FROM test12fk;
2063
+
a | b
2064
+
---+-----
2065
+
1 | abc
2066
+
2 | ABC
2067
+
(2 rows)
2068
+
2069
+
DROP TABLE test12pk, test12fk;
2070
+
CREATE TABLE test12pk (x text COLLATE case_insensitive PRIMARY KEY);
2071
+
CREATE TABLE test12fk (a int, b text COLLATE case_insensitive REFERENCES test12pk (x) ON UPDATE CASCADE);
2072
+
INSERT INTO test12pk VALUES ('abc');
2073
+
INSERT INTO test12fk VALUES (1, 'abc'), (2, 'ABC');
2074
+
UPDATE test12pk SET x = 'ABC' WHERE x = 'abc'; -- ok
2075
+
SELECT * FROM test12pk;
2076
+
x
2077
+
-----
2078
+
ABC
2079
+
(1 row)
2080
+
2081
+
SELECT * FROM test12fk; -- was updated
2082
+
a | b
2083
+
---+-----
2084
+
1 | ABC
2085
+
2 | ABC
2086
+
(2 rows)
2087
+
2088
+
DROP TABLE test12pk, test12fk;
2027
2089
-- partitioning
2028
2090
CREATE TABLE test20 (a int, b text COLLATE case_insensitive) PARTITION BY LIST (b);
2029
2091
CREATE TABLE test20_1 PARTITION OF test20 FOR VALUES IN ('abc');
0 commit comments