Skip to content

Commit 8456f4b

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

File tree

3 files changed

+22
-11
lines changed

3 files changed

+22
-11
lines changed

load_into_pg.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ def _getTableKeys(table):
150150
]
151151
return keys
152152

153-
def handleTable(table, keys, dbname, mbDbFile, mbHost, mbPort, mbUsername, mbPassword):
153+
def handleTable(table, insertJson, createFk, dbname, mbDbFile, mbHost, mbPort, mbUsername, mbPassword):
154154
"""Handle the table including the post/pre processing."""
155155
keys = _getTableKeys(table)
156156
dbFile = mbDbFile if mbDbFile is not None else table + '.xml'
@@ -309,8 +309,8 @@ def handleTable(table, keys, dbname, mbDbFile, mbHost, mbPort, mbUsername, mbPas
309309
if not args.with_post_body:
310310
specialRules[('Posts', 'Body')] = 'NULL'
311311

312-
choice = input('This will drop the {} table. Are you sure [y/n]?'.format(table))
312+
choice = input('This will drop the {} table. Are you sure [y/n]? '.format(table))
313313
if len(choice) > 0 and choice[0].lower() == 'y':
314-
handleTable(table, keys, args.insert_json, args.dbname, args.file, args.host, args.port, args.username, args.password)
314+
handleTable(table, args.insert_json, args.foreign_keys, args.dbname, args.file, args.host, args.port, args.username, args.password)
315315
else:
316316
six.print_("Cancelled.")

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

+8-3
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);
4-
ALTER TABLE Votes ADD CONSTRAINT fk_votes_postid FOREIGN KEY (postid) REFERENCES posts (id);
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+
--
9+
ALTER TABLE Votes ADD CONSTRAINT fk_votes_postid FOREIGN KEY (postid) REFERENCES posts (id) NOT VALID;

0 commit comments

Comments
 (0)