Welcome to the Question2Answer Q&A. There's also a demo if you just want to try it out.
0 votes
649 views
in Q2A Core by
I am getting the following error:
PHP Question2Answer MySQL query error 1452: Cannot add or update a child row: a foreign key constraint fails
(`islamiqa`.`qa_uservotes`, CONSTRAINT `qa_uservotes_ibfk_2` FOREIGN KEY (`userid`) REFERENCES `qa_users` (`userid`) ON DELETE CASCADE) -
Query: INSERT INTO qa_uservotes (postid, userid, vote, flag, votecreated) VALUES (1340, 0, 1, 0, NOW()) ON DUPLICATE KEY UPDATE vote=1, voteupdated=NOW()

after running the following code:

qa_db_uservote_set($parentid, 5, 1);

qa_db_hotness_update($parentid, null, true);

Any thoughts what the problem is?
Q2A version: 1.8
by
Is this custom code you’re running? Or is it happening when you do something specific in Q2A? Are you using the latest beta2?
by
It's custom code I'm running to do a data upload. I am calling the above two functions-the first generates the error.
I'm running on beta1 - I can upgrade to beta2 if you think that's the problem...
Additionally what function calls update the "no of views" tables for questions and answers?

1 Answer

0 votes
by
I cannot reproduce this at all. However, your error shows that you are trying to insert 0 as a userid into the table, and no user will have userid=0.

Your code (qa_db_uservote_set) says you're inserting 5 but presumably you are doing a loop or something that's inserting other values as well. You'll need to make sure all the rows you insert have valid user IDs.
...