Skip to content

Commit f5948ea

Browse files
committed
Do not force constraint validation by setting them as 'not valid'
1 parent 0c91790 commit f5948ea

File tree

2 files changed

+18
-7
lines changed

2 files changed

+18
-7
lines changed

sql/PostLinks_fk.sql

+11-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
1-
-- impossible to enforce so set NULL
2-
UPDATE Postlinks SET postid=NULL WHERE postid NOT IN (SELECT DISTINCT id FROM Posts);
3-
ALTER TABLE Postlinks ADD CONSTRAINT fk_postlinks_postid FOREIGN KEY (postid) REFERENCES posts (id);
4-
UPDATE Postlinks SET relatedpostid=NULL WHERE relatedpostid NOT IN (SELECT DISTINCT id FROM Posts);
5-
ALTER TABLE Postlinks ADD CONSTRAINT fk_postlinks_relatedpostid FOREIGN KEY (relatedpostid) REFERENCES posts (id);
1+
-- impossible to enforce these constraints, set as 'not valid' to disable
2+
-- initial test.
3+
--
4+
-- These constaints can be forced running the following queries:
5+
-- UPDATE Postlinks SET postid=NULL WHERE postid NOT IN (SELECT DISTINCT id FROM Posts);
6+
-- ALTER TABLE Postlinks VALIDATE CONSTRAINT fk_postlinks_postid;
7+
--UPDATE Postlinks SET relatedpostid=NULL WHERE relatedpostid NOT IN (SELECT DISTINCT id FROM Posts);
8+
-- ALTER TABLE Postlinks VALIDATE CONSTRAINT fk_postlinks_relatedpostid;
9+
--
10+
ALTER TABLE Postlinks ADD CONSTRAINT fk_postlinks_postid FOREIGN KEY (postid) REFERENCES posts (id) NOT VALID;
11+
ALTER TABLE Postlinks ADD CONSTRAINT fk_postlinks_relatedpostid FOREIGN KEY (relatedpostid) REFERENCES posts (id) NOT VALID;

sql/Votes_fk.sql

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
ALTER TABLE Votes ADD CONSTRAINT fk_votes_userid FOREIGN KEY (userid) REFERENCES users (id);
2-
-- impossible to enforce so set NULL
3-
UPDATE Votes SET postid=NULL WHERE postid NOT IN (SELECT DISTINCT id FROM Posts);
2+
-- impossible to enforce this constraint, set as 'not valid' to disable
3+
-- initial test.
4+
--
5+
-- This constaint can be forced running the following queries:
6+
-- UPDATE Votes SET postid=NULL WHERE postid NOT IN (SELECT DISTINCT id FROM Posts);
7+
-- ALTER TABLE Botes VALIDATE CONSTRAINT fk_votes_postid;
8+
--
49
ALTER TABLE Votes ADD CONSTRAINT fk_votes_postid FOREIGN KEY (postid) REFERENCES posts (id);

0 commit comments

Comments
 (0)