New to sql and postgres so sorry if this turns out to be a noobish question.
I have two tables:
CREATE TABLE usertable (
user_id VARCHAR PRIMARY KEY,
...);
CREATE TABLE hasFriend(
userID VARCHAR,
friendID VARCHAR ,
PRIMARY KEY (userID,friendID),
FOREIGN KEY (userID) REFERENCES usertable(user_id),
FOREIGN KEY (friendID) REFERENCES usertable(user_id));
Now I'm trying to run a python script to parse through a large json file that populates the usertable and friendtable. The parsing and insertion fun for user works correctly and closes the json file after finishing. After this function finishes, the friend function is called and the json is reopened from the top of the json file. Here is the code for the friend function:
def insert2FriendTable():
with open('yelp_user.JSON','r') as f:
line = f.readline()
count_line = 0
try:
#edited out the connect statement since it has personal info on it.
conn = psycopg2.connect(...)
except:
print('Unable to connect to the database!')
cur = conn.cursor()
while line:
data = json.loads(line)
for friend in data['friends']:
sql_str="INSERT INTO hasFriend (userID, friendID) VALUES ('" + data['user_id'] + "','" + friend + "');"
try:
cur.execute(sql_str)
except Exception as error:
print("Insert to hasFriend failed!", error)
return
When I run this it runs into the error:
Insert to hasFriend failed! duplicate key value violates unique constraint "hasfriend_pkey"
DETAIL: Key (userid, friendid)=(om5ZiponkpRqUNa3pVPiRg, U_sn0B-HWdTSlHNXIl_4XA) already exists.
Here is an example row from the json file. This is the first row on the file so friendfunction is crashing on that first error by looking at the id's from the error
{"average_stars": 3.94, "compliment_cool": 1556, "compliment_cute": 211, "compliment_funny": 1556, "compliment_hot": 1285, "compliment_list": 101, "compliment_more": 134, "compliment_note": 1295, "compliment_photos": 162, "compliment_plain": 2134, "compliment_profile": 74, "compliment_writer": 402, "cool": 40110, "elite": [2014, 2017, 2011, 2012, 2015, 2009, 2013, 2007, 2016, 2006, 2010, 2008], "fans": 835, "friends": ["U_sn0B-HWdTSlHNXIl_4XA", "pnfVIB7UhvCQ7X2K0Q2XIw", "jVYzrVblDFSuL3GHtt8ZSA", "Z7bpqY89ZiBHXdo7UN1kiw", "8Aqr35f254lOeitNowt7ig", "zjcN27kCVeK8K2ONe9Qt4g", "8drMKNHWavs2g6uf0pLtvg", "_K2ViyfmVq6nzIitR0TIlg", "rUV1FUhji5xMjNBpcq5SXg", "yrGIgk5eaWy-eewLNv4KHQ", "3Vd_ATdvvuVVgn_YCpz8fw", "ebC_pH92K4uxyDenoXb5bg", "RJrGgtBXkpX2oEHM4hSqXg", "sdpIz4-s15T239CZ4Bd6Ag", "A0j21z2Q1HGic7jW6e9h7A", "AvC5XQAElcGAAn_Wr5auEg", "JlkHKBnHKdK8Tpls0AF5Aw", "8AG5MctcxTjP4svmUrt0yQ", "bKxdvn7KpmWjMzlmBvp-Xw", "VVMS74JyUk2h53yfC-xNsA", "-ro7OG3jjCSKnF6OJinKjg", "K7thO1n-vZ9PFYiC7nTR2w", "pRBzWnFzaCEtqhYyJ2ZTDQ", "nwESZ8e-KzXt2fKkOuRdIQ", "WNZfkL4DBspueoGSUOMAqA", "uU6fQWadr7Hx_MP0Vmy3kQ", "XiLxIJThWsE0x4d0IeSPsg", "Y9LBTbwO4g0BmdBIi0D3CA", "jGbj8fl575EIQJcfaA1FKQ", "nxWrhF_hyX0wwjrEkQX8uQ", "670k6Gr6V4VqLIKtVEmDuQ", "o5STsEtfvD1Ig0J7Z-1uxA", "x13yoEggBL0pIE7-KMnhDQ", "rCx7tb3toOJUsvdOeqYY0g", "nkN_do3fJ9xekchVC-v68A", "KzHRsFwryS7b5Fog8kkkGA", "_pBzBgtCTN9PNUPfgPDI8A", "PU5QaMADa6N_9ZoQ04ZjOw", "FkfpHzqoDRChwOYhA6NPnQ", "CaQy-zz10ajG7KkNSbXi5w", "brQ7OjB6f9nXWGk45A9A3g"], "funny": 10882, "name": "Andrea", "review_count": 2559, "useful": 83681, "user_id": "om5ZiponkpRqUNa3pVPiRg", "yelping_since": "2006-01-18"}
I have no earthly idea how to fix this. Any advice is helpful. Thanks in advance!